在使用 Thymeleaf 时,我发现th:remove
标记并未完全删除项目。在我的例子中,我正在迭代一些对象的列表并获取它们的属性:
这是Thymeleaf代码:
<ol class="breadcrumb">
<li th:each="category : ${dish.categories}">
<a href="#" th:text="${category.name}">Category #</a>
</li>
</ol>
呈现以下HTML:
<ol class="breadcrumb">
<li><a href="#">Category 1</a></li>
<li><a href="#">Category 2</a></li>
</ol>
但是当我使用th:remove
标记时,会渲染空行而不是删除的元素。每个th:remove
标记都会生成每一个新行:
<ol class="breadcrumb">
<li th:each="category : ${dish.categories}">
<a href="#" th:text="${category.name}">Category #</a>
</li>
<li th:remove="all"><a href="#">Category 2</a></li>
<li th:remove="all"><a href="#">Category 3</a></li>
</ol>
将呈现结果:
<ol class="breadcrumb">
<li><a href="#">Category 1</a></li>
<li><a href="#">Category 2</a></li>
</ol>
是否可以在不生成这些空行的情况下使用<th:remove>
?
答案 0 :(得分:3)
否即可。 <th:remove>
将始终留空行。
在thymeleaf问题跟踪器https://github.com/thymeleaf/thymeleaf/issues/108
上请求了删除遗留空白的能力功能请求被拒绝,因此无法在百万美元核心中使用。也许有人会将其作为一个额外的&#34; (自定义方言)。这是该问题的引用。
Thymeleaf 3.0现在包含一个后处理器API(参见#400和#401)。空白折叠实用程序看起来像是后处理器的完美用例,由自定义方言提供。
将关闭此视为拒绝,因为我认为它不应该是核心或标准方言的一部分。