Elemente ein- und ausblenden

Elemente beim Rendering entfernen

Sobald das Thymeleaf gerendert wird, kann man th:remove einsetzen, um DOM-Elemente zu entfernen. Diese sind zwar sichtbar, wenn das Dokument statisch geöffnet wird, aber unsichtbar, sobald es gerendert wurde. Beispiel:

<tr th:remove="all">
  Alles hier drinn wird verschwinden
</tr>

And what about that all value in the attribute, what does it mean? Well, in fact th:remove can behave in five different ways, depending on its value:

  • all : Remove both the containing tag and all its children.
  • body : Do not remove the containing tag, but remove all its children.
  • tag : Remove the containing tag, but do not remove its children.
  • all-but-first : Remove all children of the containing tag except the first one.
  • none : Do nothing. This value is useful for dynamic evaluation.

Ein ähnliches Verhalten kann man auch mit "parser-level comment blocks" erreichen:

<!--/*-->
<div>
  you can see me only before thymeleaf processes me!
</div>
<!--*/-->

Elemente beim Rendering anzeigen

Auch der Anwendungsfall für ein umgekehrtes Verhalten existiert:

marked to be comments when the template is open statically (i.e. as a prototype), but considered normal markup by Thymeleaf when executing the template.

<span>hello!</span>
  <!--/*/
    <div th:text="${...}">
    ...
    </div>
  /*/-->
<span>goodbye!</span>

Auch ein th:block kann verwendet werden. Alle Expressions in einem Block werden ausgeführt, doch wenn das Template gerendert wird, so verschwindet der th:block. So kann man zum Beispiel das verwenden von einem

innerhalb einer verhindern (ungültige Syntax):

<table>
  <!--/*/ <th:block th:each="user : ${users}"> /*/-->
    <tr>
      <td th:text="${user.login}">...</td>
      <td th:text="${user.name}">...</td>
    </tr>
    <tr>
      <td colspan="2" th:text="${user.address}">...</td>
    </tr>
  <!--/*/ </th:block> /*/-->
</table>

results matching ""

    No results matching ""