我需要有条件地关闭我的 Thymeleaf
中的标签模板。比如说,在迭代一些元素集合时,我必须将其中一些元素的系列包装成单个 <div>
:
<div>...element1, element2, element3...</div>
<div>...element4...</div>
<div>...element5, element6...</div>
如果存在某种有条件标记关闭的方式,则可以将其存档。但我显然不能写 </div th:if="...">
。如果是 jsp
我可以轻松地写出如下内容:
<%if (condition) {%></div><%}%>
有什么想法可以解决这个问题吗?
编辑准确地说,我的元素不仅仅是字符串,它们是复杂的内部 html block 。
最佳答案
我认为最好将数据表示为单独的列表,正如您在 your previous answer 中提到的那样。
但即使出于好奇,也有一个丑陋的解决方法来实现类似于<%if (condition) {%></div><%}%>
的东西。 ,正如你所问的。
诀窍是将标签生成为转义文本:
<th:block th:if="${openTagCondition}" th:utext="'<div>'" />
<th:block th:if="${colseTagCondition}" th:utext="'</div>'" />
这只是出于好奇。我不建议使用此解决方法,因为它非常不可读,损害可维护性,并且您可能会留下不平衡的标签。
关于spring - Thymeleaf 中的有条件关闭标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36747620/