- 1 Introducing Thymeleaf
- 2 The Good Thymes Virtual Grocery
- 3 Using Texts
- 4 Standard Expression Syntax
- 5 Setting Attribute Values
- 6 Iteration
- 7 Conditional Evaluation
- 8 Template Layout
- 9 Local Variables
- 10 Attribute Precedence
- 11 Comments and Blocks
- 12 Inlining
- 13 Textual template modes
- 14 Some more pages for our grocery
- 15 More on Configuration
- 16 Template Cache
- 17 Decoupled Template Logic
- 18 Appendix A: Expression Basic Objects
- 19 Appendix B: Expression Utility Objects
- 20 Appendix C: Markup Selector Syntax
11 Comments and Blocks
11.1. Standard HTML/XML comments
Standard HTML/XML comments <!-- ... -->
can be used anywhere in Thymeleaf templates. Anything inside these comments won’t be processed by Thymeleaf, and will be copied verbatim to the result:
<!-- User info follows -->
<div th:text="${...}">
...
</div>
11.2. Thymeleaf parser-level comment blocks
Parser-level comment blocks are code that will be simply removed from the template when Thymeleaf parses it. They look like this:
<!--/* This code will be removed at Thymeleaf parsing time! */-->
Thymeleaf will remove everything between <!--/*
and */-->
, so these comment blocks can also be used for displaying code when a template is statically open, knowing that it will be removed when Thymeleaf processes it:
<!--/*-->
<div>
you can see me only before Thymeleaf processes me!
</div>
<!--*/-->
This might come very handy for prototyping tables with a lot of <tr>
’s, for example:
<table>
<tr th:each="x : ${xs}">
...
</tr>
<!--/*-->
<tr>
...
</tr>
<tr>
...
</tr>
<!--*/-->
</table>
11.3. Thymeleaf prototype-only comment blocks
Thymeleaf allows the definition of special comment blocks marked to be comments when the template is open statically (i.e. as a prototype), but considered normal markup by Thymeleaf when executing the template.
<span>hello!</span>
<!--/*/
<div th:text="${...}">
...
</div>
/*/-->
<span>goodbye!</span>
Thymeleaf’s parsing system will simply remove the <!--/*/
and /*/-->
markers, but not its contents, which will be left therefore uncommented. So when executing the template, Thymeleaf will actually see this:
<span>hello!</span>
<div th:text="${...}">
...
</div>
<span>goodbye!</span>
As with parser-level comment blocks, this feature is dialect-independent.
11.4. Synthetic th:block
tag
Thymeleaf’s only element processor (not an attribute) included in the Standard Dialects is th:block
.
th:block
is a mere attribute container that allows template developers to specify whichever attributes they want. Thymeleaf will execute these attributes and then simply make the block, but not its contents, disappear.
So it could be useful, for example, when creating iterated tables that require more than one <tr>
for each element:
<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>
And especially useful when used in combination with prototype-only comment blocks:
<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>
Note how this solution allows templates to be valid HTML (no need to add forbidden <div>
blocks inside <table>
), and still works OK when open statically in browsers as prototypes!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论