有条件地关闭Thymeleaf中的标签

编程入门 行业动态 更新时间:2024-10-08 05:26:52
本文介绍了有条件地关闭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中的标签

本文发布于:2023-11-30 12:43:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   标签   Thymeleaf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!