使用百里叶将TH:Each与模板元素相结合 [英] Combine th:each with templating element using Thymeleaf
本文介绍了使用百里叶将TH:Each与模板元素相结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个‘product’列表,我希望使用html模板将其显示为行表列表。
html模板如下所示:
<tr th:fragment="productTemplate">
<td th:text="${productName}">product name</td>
<td th:text="${productprice}>product price</td>
</tr>
以下是我所做的:
<table>
<tr th:each="product : ${products}" th:substituteby="product :: productTemplate" th:with="productName=*{name}, productPrice=*{price}" />
</table>
如果我使用th:include
,则每个tr
都嵌套了tr
如果我使用th:substituteby
,则替换优先于th:each
我找不到用其他循环项替换我的循环项的方法。
有人有解决方案吗?
推荐答案
我知道了:
<table>
<tr th:each="product : ${products}" th:include="product :: productTemplate"
th:with="productName=${product.name}, productPrice=${product.price}"
th:remove="tag" />
</table>
在这里,我们可以将模板类保留在tr元素上(这正是我想要的)
<tbody th:fragment="productTemplate">
<tr class="my-class">
<td th:text="${productName}">product name</td>
<td th:text="${productPrice}">product price</td>
</tr>
</tbody>
结果如下:
<table>
<tr class="my-class">
<td>Lettuce</td>
<td>12.0</td>
</tr>
<tr class="my-class">
<td>Apricot</td>
<td>8.0</td>
</tr>
</table>
感谢official thymeleaf forum中的danielfernandez
这篇关于使用百里叶将TH:Each与模板元素相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文