Thymeleaf學習記錄(7)--頁面引入/片段引入


1.為頁面添加footer

Templates文件夾下新建HTML文件:

 1 <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
 2 
 3 <html xmlns="http://www.w3.org/1999/xhtml"
 4       xmlns:th="http://www.thymeleaf.org">
 5 
 6 <body>
 7 
 8 <div th:fragment="copy">
 9     &copy; 2011 The Good Thymes Virtual Grocery
10 </div>
11 
12 </body>
13 
14 </html>

在主文件添加

1 <div th:include="footer :: copy"></div>

即可。

運行結果如下:

 

2.th:includeth:replace之間的區別

th:include將片段的內容包含在其主機標簽中,但th:replace實際上將用片段替換主機標簽

3.可參數化的片段簽名

前台插入代碼:

1 <div th:fragment="frag (a,b)">
2     <p th:text="hello + ${a} + ' - ' + ${b}">...</p>
3 </div>
4 
5 <div th:include="::frag (zhang,san)">...</div>
6 <div th:include="::frag (a=li,b=si)">...</div>

運行結果如下:

3.文本內聯

采用inline關鍵字可以將表達式嵌入文本。

以下兩種方式等價:

1 <h1>Hello : <b th:text="${user.name}">姓名</b></h1>
2 <p th:inline="text">Hello, [[${user.name}]]!</p>

運行結果:

此外,還可以簽入Js文件

1 <script th:inline="javascript">
2 /*<![CDATA[*/
3     ...
4 
5     var username = /*[[${session.user.name}]]*/ 'Sebastian';
6 
7     ...
8 /*]]>*/
9 </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM