In django I can do:
<head>
{% block content %}
(....) ---------> content from a partial template goes here.
{% endblock %}
(....) ---------> I can add aditional stuff here if I need to.
<head>
wherever I like. (example: use the same head in all templates but one page requires an aditional css file or script)
I understand that In thymeleaf I need to define an entire tag as a fragment to which I can no longer add anything else and need to use as is.
My question is how would I go about to achieve the above example in thymeleaf?
Create a folder under resources/templates called fragments. After create a file fragment.html then in your head:
<head>
<div th:replace="fragments/fragment :: fragment"></div>
(....) ---------> you can add aditional stuff here if you need to.
<head>
And in your fragment.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<div th:fragment="fragment" th:remove="tag">
(....) ---------> content from a partial template goes here.
</div>
</body>
</html>
th:replace will actually substitute the host tag by the fragment’s
th:remove="tag" removed the container div of the fragment included
As a result you should get:
<head>
(....) ---------> content from a partial template goes here.
(....) ---------> you can add aditional stuff here if you need
<head>