michal.jakubeczy
michal.jakubeczy

Reputation: 9469

unnecessary <html> element added when th:replace

I am using th:replace to include templates in Thymeleaf like this

master.html

<div class="faq-wrapper">
    <div th:each="faq : ${faqs}" th:remove="tag">
        <div th:replace="content-faq-card"></div>
    </div>
</div>

content-faq-card.html

<div class="faq-card">
    ...
</div>

I initialize my templateRenderer like this:

ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();

templateResolver.setTemplateMode("LEGACYHTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix("/WEB-INF/");
templateResolver.setSuffix(".html");
templateResolver.setCacheable(false);

TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);

return templateEngine;

It works fine, but if I look into generated HTML I see that everytime I include content-faq-card there is extra

<HTML><head></head><BODY>
<div class="faq-card">
    ...
</div>
</BODY></HTML>

It renders correctly, but HTML is not valid. Why does this wrapping happen? Is there any way to avoid this?

Upvotes: 0

Views: 33

Answers (1)

user7655213
user7655213

Reputation:

You need to define your <div class="faq-card"> as <div th:fragment="faq-card">, and reference it as <div th:replace="content-faq-card :: faq-card"></div>.

Upvotes: 1

Related Questions