九、局部變量(local variable)
之前在th:each中遇到過局部變量
<tr th:each="prod : ${prods}"> ... </tr>
其中prod就是局部變量。
除此之外,thymeleaf提供了另外一種聲明方式,通過使用th:each,語法如下:
<div th:with="firstPer=${persons[0]}">
<p>The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.</p>
</div>
多個可用逗號隔開:
<div th:with="firstPer=${persons[0]},secondPer=${persons[1]}">
<p>The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.</p>
<p> But the name of the second person is <span th:text="${secondPer.name}">Marcus Antonius</span>. </p>
</div>
th:with屬性允許用定義在同一屬性內的變量
<div th:with="company=${user.company + ' Co.'},account=${accounts[company]}">...</div>
<p> Today is: <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 february 2011</span>
</p>
Well, 我們想要 "MMMM dd'','' yyyy" 的日期格式,那么我們可以加下面的句子到 home_en.properties
date.format=MMMM dd'','' yyyy
<p> Today is: <span th:with="df=#{date.format}" th:text="${#calendars.format(today,df)}">13 February 2011</span>
</p>
th:with優先級高於 th:text,所以我們可以放在一個標簽內
十、屬性優先級(Atrribute Precedence)
十一、注釋 (Comments and Blocks)
11.1 <!-- ... --> 同HTML/XML的注釋
<!-- User info follows -->
<div th:text="${...}"> ... </div>
11.2 thymeleaf解析器注釋
thymeleaf解析的時候會被注釋,靜態打開時會顯示
單行 <!--/* ... */-->
<!--/* This code will be removed at thymeleaf parsing time! */-->
多行時:
<!--/*--> ... ... <!--/*-->
<!--/*-->
<div> you can see me only before thymeleaf processes me! </div>
<!--*/-->
11.3 靜態解析時被注釋,thymeleaf解析時會移除<!--/*/ 和 /*/-->標簽對,內容保留
<span>hello!</span>
<!--/*/ <div th:text="${...}"> ... </div> /*/-->
<span>goodbye!</span>
這個注釋在寫th:block的時候很有用哦
<table>
<!--/*/ <th:block th:each="user : ${users}"> /*/-->
<tr>
<td th:text="${user.login}">...</td>
<td th:text="${user.name}">...</td>
</tr>
<tr>
<td colspan="2" th:text="${user.address}">...</td>
</tr>
<!--/*/ </th:block> /*/-->
</table>
11.4 th:block 適用於比如說要循環兩個<tr>
it could be useful, for example, when creating iterated tables that require more than one <tr> for each element