📜 ⬆️ ⬇️

Thymeleaf Tutorial: Chapter 10. Attribute Priority

Table of contents

10 Attribute Priority


What happens when you write more than one th: * attribute in one tag? For example:

<ul> <li th:each="item : ${items}" th:text="${item.description}">Item description here...</li> </ul> 

We expect that the th: each attribute will be executed before the th: text so that we get the desired results, but given the fact that the HTML / XML standards have no meaning for the order of the attribute in the tag, the priority mechanism must be set in the attributes themselves , to be sure that this will work as expected.

Thus, all Thymeleaf attributes define a numeric value that sets the order in which they are executed in the tag. This order:
')
OrderFunctionalityAttributes
oneInclusion of fragmentsth: insert
th: replace
2Fragment iterationth: each
3Conditional executionth: if
th: unless
th: switch
th: case
fourLocal variable definitionth: object
th: with
fiveThe main attribute modificationth: attr
th: attrprepend
th: attrappend
6Specific attribute modificationth: value
th: href
th: src
...
7Text (tag body modification)th: text
th: utext
eightFragment definitionth: fragment
9Removing a fragmentth: remove

This priority mechanism means that the above iteration fragment will produce exactly the same results if the attribute position is inverted (although it will be slightly less readable):

 <ul> <li th:text="${item.description}" th:each="item : ${items}">Item description here...</li> </ul> 

Source: https://habr.com/ru/post/351854/


All Articles