2013-06-19 66 views
1

有沒有使用Thymeleaf製作複合組件(比如在JSF中)的方法?我發現如何將參數發送到可以使用表達式語言檢索的片段。但我還沒有想出如何發送標記碎片,就像你可以在JSF中。百里香複合組件

例如,我有幾個頁面在左邊有非常相似的菜單。我希望能夠在我的所有頁面上使用單個片段,但會傳遞一些標記以顯示在菜單底部。有些頁面必須在底部顯示文本,有些頁面必須顯示文本,例如,它實際上比這更復雜。

<div class="menu" th:frament="menu"> 
    <a th:text="${menuItem1}"></a> 
    <a th:text="${menuItem2}"></a> 
    <a th:text="${menuItem3}"></a> 
    <markup sent as parameter /> <!-- how do I do this? --> 
</div> 



<div th:substitueBy="template :: menu" th:with="menuItem1=item1, menuItem2:item2, menuItem3:item3"> 
    <markup to be sent as parameter /> <!-- this does not work --> 
</div> 

回答

0

你可以做這樣的事情:

但你必須寫所有菜單容器屬性,每次使用它的頁面。

我真的不喜歡這種方式,但我認爲它應該工作:對

模板:

<div th:frament="menu"> 
    <a th:text="${menuItem1}"></a> 
    <a th:text="${menuItem2}"></a> 
    <a th:text="${menuItem3}"></a> 
</div> 

頁:

<div class="menu"> 
    <div th:include="template :: menu" 
     th:remove="tag" 
     th:with="menuItem1=item1, menuItem2:item2, menuItem3:item3" /> 
    <more markup/> 
</div> 
0

我也想創造具有可交換內容的複合組件。這就是我開發thymeleaf-component-dialect的原因。

有了這個話,你可以寫組件是這樣的:

<tc:content/>標籤

<div th:frament="menu"> 
    <a th:text="${menuItem1}"></a> 
    <a th:text="${menuItem2}"></a> 
    <a th:text="${menuItem3}"></a> 
    <tc:content></tc:content> 
</div> 

使用的組件創建一個組件(一thymeleaf片段)

<tc:menu> 
    <h1>Some Header</h1> 
    <p>Lorem ipsum dolor sit amet</p> 
</tc:menu> 

結果將是

<div> 
    <a>Menu Item 1 Text</a> 
    <a>Menu Item 2 Text</a> 
    <a>Menu Item 3 Text</a> 
    <h1>Some Header</h1> 
    <p>Lorem ipsum dolor sit amet</p> 
</div> 

方言是新的,仍處於阿爾法狀態。歡迎反饋。

+0

遲了幾年,但謝謝!不幸的是,我不再使用百里香,所以我無法測試你的圖書館 – Hoffmann

+0

是的,它已經很晚了。但對於也在尋找這個問題的人來說,答案可能會有所幫助。 – Serbroda