📜 ⬆️ ⬇️

Thymeleaf Tutorial: Chapter 20. Appendix C: Markup Selector Syntax

Table of contents

20 Appendix C: Markup Selector Syntax


Thymeleaf markup selectors are directly borrowed from the Thymeleaf: AttoParser parser library.

The syntax for these selectors is very similar to the syntax of selectors in XPath, CSS, and jQuery, which makes them convenient for most users. You can take a look at the complete syntax reference in the AttoParser documentation.

For example, the following selector will select each <div> with class content at each position inside the markup (note that this is not as concise as it could be, read on to know why):
')
<div th:insert="mytemplate :: //div[@class='content']">...</div> 

The basic syntax includes:


But you can use a more concise syntax:


Extended attribute selection functions:


JQuery direct selectors:


Thus, the Markup Selector expression is higher:

 <div th:insert="mytemplate :: //div[@class='content']">...</div> 

Can be written as:

 <div th:insert="mytemplate :: div.content">...</div> 

Having studied another example, do the following:

 <div th:replace="mytemplate :: myfrag">...</div> 

It will look for a fragment of the fragment th: fragment = "myfrag" (or th: ref links). But they would also search for tags with the name myfrag , if they existed (which is not in HTML).

Note the difference with:

 <div th:replace="mytemplate :: .myfrag">...</div> 

... which will actually look for any elements with class = "myfrag" , without worrying about the signatures: th: fragment (or th: ref ).

Multivalued class mapping

Markup selectors understand that the class attribute is multivalued (multivalued), and therefore allows the use of selectors for this attribute, even if the element has several class values.

For example, div.two would match

 <div class="one two three" /> 

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


All Articles