Search code examples
thymeleaf

Need a precise explanation for this thymeleaf fragment signature


I was doing a quick review of thymeleaf and I saw this UI code snippet:

<html lang="en" xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'home')}">

After some research [https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#parameterizable-fragment-signatures], I have a general idea but this part of the code I am not sure what it means:

~{::body},'home')

So I need a really clear explanation


Solution

  • It is easy to break down the structure into a hierarchy by using what thymeleaf is using as fragment selectors (One of the features of Thymeleaf Fragments is that we can grab any part of a template just using the simple selectors, through classes, ids, or simply by tags).

    "~{fragments/layout :: layout (~{::body},'home')}"
    
    1. fragments/layout -> Selects the layout.html file under fragments directory.
    2. :: layout -> Selects the layout markup fragment which is parametrized with two parameters.
    3. (~{::body},'home') -> Passes two parameters to layout markup fragment. The first one is also a markup fragment selected with ::body, the second one is basically a string valued home.

    You can understand this with an example provided here in which you can find under section 5.