標準的html/xhtml註釋
標準的html/xhtml註釋可以在模板文件中任意使用。所有在<!– –>裏面的內容都不會被thymeleaf和瀏覽器解析。
<!-- User info follows -->
<div th:text="${...}">
...
</div>
- 1
- 2
- 3
- 4
Thymeleaf級別的註釋
thymeleaf級別的註釋,指的是那些在引擎解析的時候會去掉的註釋部分。
<!--/* 這些代碼會被引擎解析時去掉 */-->
- 1
引擎會去掉所有<!–/* 和 */–>之間的內容。所以你也可以用它來顯示靜態文本。
<!--/*-->
<div>
you can see me only before thymeleaf processes me!
</div>
<!--*/-->
- 1
- 2
- 3
- 4
- 5
你可以用它來註釋其他的TR。
<table>
<tr th:each="x : ${xs}">
...
</tr>
<!--/*-->
<tr>
...
</tr>
<tr>
...
</tr>
<!--*/-->
</table>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
保留註釋塊的內容
thymeleaf中還有一種用法,在解析時用來保留註釋塊裏的內容,並去掉註釋標籤。
<span>hello!</span>
<!--/*/
<div th:text="${...}">
...
</div>
/*/-->
<span>goodbye!</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Thymeleaf解析系統會刪除< !–/ * /,/ * / –>標記,保留裏面的內容。
<span>hello!</span>
<div th:text="${...}">
...
</div>
<span>goodbye!</span>
- 1
- 2
- 3
- 4
- 5
th:block標籤
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
尤其在和保留內容的註釋同時使用時:
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11