Uses Groovy
As described in http://www.thymeleaf.org/doc/articles/layouts.html and in the Thymeleaf documentation http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#flexible-layouts-beyond-mere-fragment-insertion one can use fragments to build a common layout.
Defines the layout
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:fragment="template(title,content)">
<head>
<title th:text="${title}">Layout</title>
...
</head>
<body>
...
<div class="container">
<div th:replace="${content}">A placeholder for real content.</div>
</div>
...
</body>
</html>
Here we define a parametrized fragment th:fragment="template(title,content)"
at root level. The fragment has two parameters
th:replace="${content}"
The layout template can be used as following
<html xmlns:th="http://www.thymeleaf.org" th:replace="fragments/layout :: template('My Title',~{::content})">
...
<div class="row" th:fragment="content">
...
</div>
</html>
Dynamic fragment inclusion based on a model variable. The view name is always the name of the main layout template, the fragment is selected by a model property.
Main layout:
...
<div th:replace="${freagmentToShow} :: content">
Content
</div>
...
This approach is described in more detail by