有条件地关闭Thymeleaf中的标签 [英] Conditionally closing tag in Thymeleaf
问题描述
我需要有条件地关闭 Thymeleaf
模板中的标签。说,在迭代某些元素期间,我必须将其中的一些元素包装为单个< div>
:
I need to conditionally close tag in my Thymeleaf
template. Say, during iterating some collection of elements I have to wrap series of some of them into single <div>
:
<div>...element1, element2, element3...</div>
<div>...element4...</div>
<div>...element5, element6...</div>
如果存在某种条件的标签关闭方式,则可以将其存档。但是我显然不能写< / div th:if = ...>
。如果是 jsp
,我可以轻松地写成这样:
This could be archived if some way of conditionally tag closing would exist. But I can't obviously write </div th:if="...">
. If it would be jsp
I could easily write something like:
<%if (condition) {%></div><%}%>
任何想法如何解决此问题?
Any ideas how to solve this issue?
编辑确切地说,我的元素不仅仅是字符串,它们是复杂的内部html块。
EDIT To be precise, my elements aren't just strings, they are complex inner html blocks.
推荐答案
我找到了解决方法。应该包装为单个< div>
的一系列块应在模型中表示为单独的列表。说,我有 Element
类,它描述了我的 element
块。因此,我的模型应为:
I've found the workaround. Series of blocks which should be wrapped into single <div>
should be represented as separated lists inside model. Say, it I have Element
class which describes my element
block. So, my model should be like:
List< Element> elementGroups
,我必须为其创建双循环:
and I have to create double loop for it:
<div th:each="group : ${elementGroups}">
<th:block th:each="element : ${group}">
...
</th:block>
</div>
这篇关于有条件地关闭Thymeleaf中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!