Attribute

Ein Attribut manipulieren

<form action="subscribe.html" th:attr="action=@{/subscribe}">

oder

<input type="submit" value="Subscribe me!" th:attr="value=#{subscribe.submit}"/>

The concept is quite straightforward: th:attr simply takes an expression that assigns a value to an attribute.

Mehr als ein Attribut gleichzeitig verändern

<img src="../../images/gtvglogo.png"
th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />

Eine schönere Methode

Specifying an assignment inside an attribute’s value can be very practical, but it is not the most elegant way of creating templates if you have to do it all the time. Thymeleaf agrees with you. And that’s why in fact th:attr is scarcely used in templates. Normally, you will be using other th:* attributes whose task is setting specific tag attributes (and not just any attribute like th:attr ).

<li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>

There are quite a lot of attributes like these, each of them targeting a specific XHTML or HTML5 attribute:

(Siehe PDF auf Seite 30)

Um mehrere Attribute gleichzeitig anzupassen existieren zwei spezielle Lösungen:

  • th:alt-title will set alt and title .
  • th:lang-xmllang will set lang and xml:lang .

Ein Attribut ergänzen

Attribute können folgendermassen ergänzt werden:

<input type="button" value="Do it!" class="btn" th:attrappend="class=${' ' + cssStyle}" />

Resultat:

<input type="button" value="Do it!" class="btn warning" />

Anwendungsfall (Beispiel):

<tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? 'odd'">

Fixed-value boolean attributes

Einige HTML-Attribute verwenden nicht true oder false, sondern einen vordefinierten String. Zum Beispiel eine Checkbox mit "checked":

<input type="checkbox" name="option1" checked="checked" />

Dank Thymeleaf kann man in solchen föllen aber trotzdem boolean-Werte verwenden, so wie hier:

<input type="checkbox" name="active" th:checked="${user.active}" />

Hinweis: (Auf der Seite 32 im PDF gibt es eine Liste mit Attributen, welche diese Technik unterstützen)

results matching ""

    No results matching ""