(八)Thymeleaf的 th:* 屬性之—— 模板布局& th:with& 屬性優先級


3.7 模板布局

模板名稱:layout.html

3.7.1 th:fragment

e.g.模板名為footer.html頁面body部分如下:
<body>
  <div th:fragment="copy">
      &copy; 2011 The Good Thymes Virtual Grocery
    </div>
</body>
fragment 片段定義語法:
如th:fragment=”copy”這樣就定義了一個名為copy的fragment

3.7.2 th:include  and th:replace

<1>引入fragment的形式:    簡單地,templatename::fragmentname(不惟一)
 <div th:insert="~{footer :: copy}"></div>
equals.
<div th:insert="footer :: copy"></div>
<2>二者的區別 th:include:將fragment的內容包含進來; th:replace:用fragment替換掉所在標簽
        

3.7.3 th:remove  

一般用於將模擬數據在真實環境中移除:  
th:remove 可以以五種不同的方式行事,具體取決於它的價值
  • all:刪除包含標簽及其所有子項。
  • body:不要刪除包含的標簽,但刪除其所有的孩子。
  • tag:刪除包含的標簽,但不要刪除其子項。
  • all-but-first:除去第一個包含標簽的所有子項。
  • none: 沒做什么。該值對於動態評估是有用的。
e.g.
 <tr th:remove="all">
    <td>Mild Cinnamon</td>
    <td>1.99</td>
    <td>yes</td>
</tr>

<a href="/something" th:remove="${condition}? tag : none">Link text not to be removed</a>

3.8 th:with

模板名稱:with.html 定義局部變量

1.可一次定義多個,逗號分隔

e.g.
<div th:with="firstPer=${list[0]}">  
    <p>The name of the first person is <span th:text="${firstPer.userName}">Julius Caesar</span>.</p>  
</div>  
<div th:with="firstPer=${list[0]},secondPer=${list[1]}">  
    <p>The name of the first person is <span th:text="${firstPer.userName}">Julius Caesar</span>.</p>  
    <p> But the name of the second person is  <span th:text="${secondPer.userName}">Marcus Antonius</span>.  
    </p>  
</div>  

2.可復用

e.g.
<div th:with="company=${user.company},account=${accounts[company]}">  
    <div th:text="${company}"></div>  
    <div th:text="${account}"></div>  
</div>  

3.9 屬性優先級

  

 


免責聲明!

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



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