2016-10-03 1272 views
1

同時使用Thymeleaf我發現,th:remove標籤並未完全刪除項目。在我的例子,我遍歷,某些對象的列表,並得到他們的屬性:空格內容以HTML格式顯示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>不會產生這些空行?

回答

3

<th:remove>將始終保留空行。

這種能力來移除留下已要求對thymeleaf問題跟蹤器的空白,https://github.com/thymeleaf/thymeleaf/issues/108

的功能請求被拒絕,因此無法在thymeleaf提供的核心。也許有人會將其作爲「額外」(自定義方言)來實現。這是來自這個問題的引用。

Thymeleaf 3.0現在包含後處理器API(請參閱#400和#401)。空白摺疊實用程序看起來像後處理器的完美用例,由自定義方言提供。

由於我認爲它不應該是核心或標準方言的一部分,將會關閉此項功能。

+0

感謝您的回覆!還有一個使用自定義'ITemplateWriter'描述的解決方法。 – DimaSan