九、局部變量(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>
多個可用逗号隔開:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5iN1YjN2QDNhBTNjZjYkZTOyYzXwIjMyETMxAzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
<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>
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5iN1YjN2QDNhBTNjZjYkZTOyYzXwIjMyETMxAzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
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! */-->
多行時:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5iN1YjN2QDNhBTNjZjYkZTOyYzXwIjMyETMxAzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
<!--/*-->
...
...
<!--/*-->
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5iN1YjN2QDNhBTNjZjYkZTOyYzXwIjMyETMxAzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
<!--/*-->
<div>
you can see me only before thymeleaf processes me!
</div>
<!--*/-->
11.3 靜态解析時被注釋,thymeleaf解析時會移除<!--/*/ 和 /*/-->标簽對,内容保留
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5iN1YjN2QDNhBTNjZjYkZTOyYzXwIjMyETMxAzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
<span>hello!</span>
<!--/*/
<div th:text="${...}">
...
</div>
/*/-->
<span>goodbye!</span>
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5iN1YjN2QDNhBTNjZjYkZTOyYzXwIjMyETMxAzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
這個注釋在寫th:block的時候很有用哦
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5iN1YjN2QDNhBTNjZjYkZTOyYzXwIjMyETMxAzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
<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>
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5iN1YjN2QDNhBTNjZjYkZTOyYzXwIjMyETMxAzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
11.4 th:block 适用于比如說要循環兩個<tr>
it could be useful, for example, when creating iterated tables that require more than one <tr> for each element