Thymeleaf でどうやってフラグメント(部分テンプレート)を実現するのか書きました。 In Rails, it is called partial template, but in Thymeleaf, usually called fragment.
Environment
- Thymeleaf 2.1.4
フラグメント(部分テンプレート)を使う場合は、 we use th:include
or th:replace
. 違いは、 指定したタグの中に展開するか(th:include
)、 指定したタグを置き換えるか(th:replace)です。
Sample
Here’s sample code with th:replace
.
Write the main template first.
And create the file navigation.html
in the directory resources/common
.
Then, the tag with the attribute th:replace
will be replaced to navigation.html
content.
th:fragment
attribute enables us to compose more complicated templates. Look at Thymeleaf Page Layouts for the detail