2016-06-13 77 views
0

我在想,如果有可能,使用FXML,做模板一樣,可以用枝條在PHP(和肯定很多其他模板電機)是否有可能做模板與FXML

使用嫩枝你會有一個這樣的視圖:

佈局:

<html> 
<head> 
    <title>{% block title %}My Website{% endblock %}</title> 
</head> 

<body> 
{% block body %}{% endblock %} 
</body> 
</html> 

some_page:

{% extends "layout" } 
{% block title %}{{ parent() }} - Some page{% endblock %} 
{% block body %} 
    <div>Some content</div> 
{% endblock %} 

這會使像這樣在瀏覽器:

<html> 
<head> 
    <title>My Website - Some page</title> 
</head> 

<body> 
    <div>Some content</div> 
</body> 
</html> 

我的問題是:是否有這樣的事情可能與FXML。因此,我們不使用HTML,而是使用標準的FXML和一些特殊的標籤在子FXML文件中重新定義。

我現在有一個包含機制,但這不是我正在尋找的。包含假設你必須重新定義「塊」,即使是空文件。我想要的是一種繼承機制。

是否可以使用FXML?

+1

沒有內建的東西,但請注意,您可以加載FXML [只指定'InputStream'](http://docs.oracle.com/javase/8/javafx/api/javafx/fxml/FXMLLoader。 HTML#負載java.io.InputStream-)。所以,雖然我從來沒有嘗試過,並且開始時可能需要相當多的工作才能弄清楚如何去做,應該可以使用任何通用的Java模板庫(例如[Tiles]( https://tiles.apache.org/))即時生成FXML。 –

+0

相關類別:http://stackoverflow.com/questions/24321871/building-javafx-ui-dynamically-on-the-fly/24329725#24329725 –

+0

謝謝你的回答James_D。不幸的是,JavaFX似乎沒有預處理這種機制。這是不幸的,因爲它真的是一個很好的功能。 –

回答

0

這是我如何使用Pebble解決我的問題。在我的真實項目中使用Pebble之前,下面的例子爲我提供了一個概念證明。我保持它很簡單,但它做了證明我想實現的工作是可能的。

Layout.pebble.fxml:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.control.*?> 

<BorderPane id="page" prefWidth="1280.0" prefHeight="900.0" xmlns:fx="http://javafx.com/fxml"> 
    <top> 
     <MenuBar BorderPane.alignment="CENTER"> 
      <menus> 
       <Menu mnemonicParsing="false" text="File"> 
        <items> 
         <MenuItem mnemonicParsing="false" onAction="#exitAction" text="Exit" /> 
        </items> 
       </Menu> 

       <Menu mnemonicParsing="false" text="Help"> 
        <items> 
         <MenuItem mnemonicParsing="false" text="About" /> 
        </items> 
       </Menu> 
      </menus> 
     </MenuBar> 
    </top> 

    <center> 
     {% block body %}{% endblock %} 
    </center> 
</BorderPane> 

Index.pebble.fxml:

{% extends "templates/Layout.pebble.fxml" %} 

{% block body %} 
<VBox xmlns:fx="http://javafx.com/fxml/1"> 
    <Text text="Test" /> 
</VBox> 
{% endblock %} 

App.java:

package be.dupirefr.pebblefx; 

import java.io.File; 
import java.io.PrintWriter; 
import java.util.HashMap; 
import java.util.Map; 

import com.mitchellbosecke.pebble.PebbleEngine; 
import com.mitchellbosecke.pebble.template.PebbleTemplate; 

import be.dupirefr.pebblefx.controllers.DumbController; 
import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class App extends Application { 
    public static void main(String args[]) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     PebbleEngine engine = new PebbleEngine.Builder().build(); 
     PebbleTemplate compiledTemplate = engine.getTemplate("templates/Index.pebble.fxml"); 

     Map<String, Object> context = new HashMap<>(); 

     File file = File.createTempFile("views/Index", "fxml"); 
     file.deleteOnExit(); 

     PrintWriter writer = new PrintWriter(file); 
     compiledTemplate.evaluate(writer, context); 
     writer.close(); 

     primaryStage.setTitle("Pebble test"); 

     FXMLLoader loader = new FXMLLoader(); 

     loader.setController(new DumbController()); 
     loader.setLocation(file.toURI().toURL()); 

     BorderPane pane = loader.load(); 
     Scene scene = new Scene(pane); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
} 

過程中產生這個文件:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.control.*?> 

<BorderPane id="page" prefWidth="1280.0" prefHeight="900.0" 
    xmlns:fx="http://javafx.com/fxml"> 
    <top> 
     <MenuBar BorderPane.alignment="CENTER"> 
      <menus> 
       <Menu mnemonicParsing="false" text="File"> 
        <items> 
         <MenuItem mnemonicParsing="false" onAction="#exitAction" 
          text="Exit" /> 
        </items> 
       </Menu> 

       <Menu mnemonicParsing="false" text="Help"> 
        <items> 
         <MenuItem mnemonicParsing="false" text="About" /> 
        </items> 
       </Menu> 
      </menus> 
     </MenuBar> 
    </top> 

    <center> 
     <VBox xmlns:fx="http://javafx.com/fxml/1"> 
      <Text text="Test" /> 
     </VBox> 
    </center> 
</BorderPane> 

它被FXMLLoader成功使用。

當然,您可以使用Pebble的其他功能,但是我想看到的是它是否可以與任何類型的文件一起使用,以及FXMLLoader是否將臨時文件作爲輸入。

相關問題