java - 如何将百里香片段作为参数传递给另一个片段?
问题描述
是否可以将标签/片段传递给另一个模板thymeleaf
?
示例:我想创建一个基本tableview
布局,调用者模板应该只提供<tbody>
应该注入到 tableview 模板布局中的内容。
这可能是表格布局:
<div th:fragment="tableview (tbodyFragment)">
<table class=...>
<thead>...</thead>
<!-- the table body should be repaced -->
<tbody th:replace="${tbodyFragment}"/>
</table>
</div>
调用模板:
<tbody id="tbodyFragment">
<th:block th:each="row : ${rows}">
<tr>
<td th:text="${row.id}"/>
<td th:text="${row.firstname}"/>
<td th:text="${row.lastname}"/>
<td th:text="${row.age}" style="text-align:right"/>
</tr>
<th:block>
</tbody>
<div th:insert="~{tableview::tableview(tbodyFragment)}"/>
当然上面的语法是无效的,但你明白了。我怎么能做到这一点?
解决方案
这很简单,将片段作为 id 传递,并将其嵌套在th:insert
调用模板的标签下方:
<div th:insert="~{tableview::tableview(~{:: #tbodyFragment})}">
<tbody id="tbodyFragment">
...content here...
</tbody>
</div>
推荐阅读
- javascript - 如何删除正则表达式中的第一个数字后跟逗号?
- discord.py - 如何修复 Discord.py 中的“create_channel BAD REQUEST(状态代码:400)”
- postgresql - 如何使用 Postgres 数据库与 JHipster 一起开发?
- python - Python范围无法按预期使用变量作为步骤
- javascript - 使用 AJAX 和 jQuery 将文件路径发送到 PHP,并取回文件内容
- r - 带有ggplot2(R)的每个系列密度2d的单个时装线
- raspberry-pi - git.drogon.net 无法访问后如何克隆wiringPi
- java - 使用从 Json 响应中传递的对象填充 JTable
- laravel - 部署 Laravel 应用后出现 403 错误
- postgresql - 为什么在将本地 Postgres 还原到 AWS RDS 后 SQL 语法会发生变化?