2017-08-17 75 views
1

在Django我可以這樣做:如何添加其他內容到thymeleaf頭標記,如果它來自一個片段?

<head> 
{% block content %} 

    (....) ---------> content from a partial template goes here. 

{% endblock %} 

    (....) ---------> I can add aditional stuff here if I need to. 

<head> 

的地方,我喜歡。 (例如:在所有模板中使用相同的頭,但一頁需要附加的css文件或腳本)

我知道在百里香葉中,我需要將整個標籤定義爲片段,我不能再添加其他任何東西,需要按原樣使用。

我的問題是我將如何去實現上面的例子在thymeleaf?

回答

1

在名爲fragments的資源/模板下創建一個文件夾。創建後一個文件fragment.html然後在你的腦袋:

<head> 

    <div th:replace="fragments/fragment :: fragment"></div> 


    (....) ---------> you can add aditional stuff here if you need to. 

<head> 

而在你fragment.html:

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head></head> 
<body> 
    <div th:fragment="fragment" th:remove="tag"> 
    (....) ---------> content from a partial template goes here. 
    </div> 
</body> 
</html> 

日:更換實際上將片段的

替代主機標籤

th:remove =「tag」刪除了包含的片段的容器div

因此你應該得到:

<head> 

    (....) ---------> content from a partial template goes here. 

    (....) ---------> you can add aditional stuff here if you need 

<head> 
+0

美麗!正是我在找什麼!謝謝 – PaulB

相關問題