Let's say I have two Thymeleaf templates:
index.html:
<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
<div th:replace="fragments/main :: content"></div>
</section>
<footer>bar</footer>
</body>
</html>
fragments/main.html:
<!DOCTYPE html>
<html>
<head></head>
<body>
<div th:fragment="content">
<p>This is the main content.</p>
</div>
</body>
</html>
How do I prevent Tymeleaf from including the div
that defines the fragment in the composited output? That is, how do I get Thymleaf to generate the following output:
<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
<p>This is the main content.</p>
</section>
<footer>bar</footer>
</body>
</html>
Instead of:
<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
<div>
<p>This is the main content.</p>
</div>
</section>
<footer>bar</footer>
</body>
</html>
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With