Use a Thymeleaf template without including the fragment definition element?

Alternatively, you could try using th:block instead of div in main.html, like so:

<!DOCTYPE html>
<html>
<head></head>
<body>
<th:block th:fragment="content">
    <p>This is the main content.</p>
</th:block>
</body>
</html>

Note, however, that this will slightly change the way main.html looks when viewed as raw HTML without preprocessing by Thymeleaf.


Yet another simple way to acomplish this is using th:replace="fragments/main :: content/text()"> as shown here: https://github.com/thymeleaf/thymeleaf/issues/451

Your fragments/main.html remains the same

The th:replace attribute changes in the index.html:

<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
    <div th:replace="fragments/main :: content/text()"></div>
</section>
<footer>bar</footer>
</body>
</html>

Perhaps this method was not available 5 years ago when the question was asked, but I wanted to add it here for anyone who stumbles upon this question looking for a solution. I was having a similar problem and the th:remove="tag" didn't work for me.


Use th:remove="tag". (documentation)

fragments/main.html:

<!DOCTYPE html>
<html>
<head></head>
<body>
<div th:fragment="content" th:remove="tag">
    <p>This is the main content.</p>
</div>
</body>
</html>

Tags:

Thymeleaf