2017-03-19 41 views
0

我用一個「頭」片段與Thymeleaf存儲我的CSS鏈接。我可以改變Thymeleaf「包含」優先權嗎?

我也有一個獨特的css文件,我想只適用於給定的頁面。我第一次嘗試做到這一點是下面的方法。

<head th:include="fragments/head :: head"> 
    <link rel="stylesheet" th:href="@{/css/create-post.css}" /> 
</head> 

但是似乎從片段中的CSS文件插入下head標籤的當前內容。這是一個問題,因爲在create-post.css中希望重寫一些樣式。

有沒有辦法改變Thymeleaf如何插入頭標籤的優先順序?

回答

0

我不知道如果我理解正確的,但讓我給它一些嘗試......

建議1)我在其中一些javascript-S都包含在每一頁上一個小項目,有些頁面包含頁面特定的腳本。這裏是頭片段的定義:

<head th:fragment="common_header(title, scripts)"> 

    <title th:replace="${title}">A title</title> 

    <!-- Common scripts --> 
    <script 
     src="/js/jquery/jquery.min.js" 
     th:src="@{/js/jquery/jquery.min.js}"> 
    </script> 

    ... 

    <th:block th:replace="${scripts}"> 
    </th:block> 

</head> 

這裏是一個特定的網頁,其中不僅需要jQuery的同時也爲驗證碼支持其他腳本:

<head th:replace="/fragments/commonheader :: common_header(~{::title},~{::script})"> 

    <title>Register a new user</title> 

    <script th:src="@{https://www.google.com/recaptcha/api.js}"></script> 

</head> 

建議2)除了th:include有是th:replace。而th:include將把片段的內容包含到它的主機標籤中,th:replace實際上將用片段的替代主機標籤。詳情here

我希望這些想法可以幫助。

相關問題