使用Thymeleaf模板而不包含片段定义元素?

Jam*_*ers 23 thymeleaf

假设我有两个Thymeleaf模板:

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>
Run Code Online (Sandbox Code Playgroud)

fragments/main.html:

<!DOCTYPE html>
<html>
<head></head>
<body>
<div th:fragment="content">
    <p>This is the main content.</p>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

如何防止Tymeleaf div在合成输出中包含定义片段的内容?也就是说,我如何让Thymleaf生成以下输出:

<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
    <p>This is the main content.</p>
</section>
<footer>bar</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

代替:

<!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>
Run Code Online (Sandbox Code Playgroud)

Iwo*_*ski 46

使用th:remove="tag".(文件)

片段/ 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>
Run Code Online (Sandbox Code Playgroud)


Mic*_*ski 30

或者,您可以尝试使用th:block而不是divin main.html,如下所示:

<!DOCTYPE html>
<html>
<head></head>
<body>
<th:block th:fragment="content">
    <p>This is the main content.</p>
</th:block>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

但请注意,这将略微改变在main.html没有Thymeleaf预处理的情况下作为原始HTML查看时的外观.