天天看點

thymeleaf 局部變量、屬性優先級、注釋

九、局部變量(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>

多個可用逗号隔開:

thymeleaf 局部變量、屬性優先級、注釋

<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>

thymeleaf 局部變量、屬性優先級、注釋

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)

thymeleaf 局部變量、屬性優先級、注釋

 十一、注釋 (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! */-->

多行時:

thymeleaf 局部變量、屬性優先級、注釋

<!--/*-->

...

...

<!--/*-->

thymeleaf 局部變量、屬性優先級、注釋

<!--/*-->

<div>

you can see me only before thymeleaf processes me!

</div>

<!--*/-->

11.3 靜态解析時被注釋,thymeleaf解析時會移除<!--/*/ 和 /*/-->标簽對,内容保留

thymeleaf 局部變量、屬性優先級、注釋

<span>hello!</span>

<!--/*/

<div th:text="${...}">

...

</div>

/*/-->

<span>goodbye!</span>

thymeleaf 局部變量、屬性優先級、注釋

這個注釋在寫th:block的時候很有用哦

thymeleaf 局部變量、屬性優先級、注釋

<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>

thymeleaf 局部變量、屬性優先級、注釋

11.4 th:block 适用于比如說要循環兩個<tr>

it could be useful, for example, when creating iterated tables that require more than one <tr> for each element