假设我有两个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>
fragment / main.html :
<!DOCTYPE html>
<html>
<head></head>
<body>
<div th:fragment="content">
<p>This is the main content.</p>
</div>
</body>
</html>
如何防止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>
代替:
<!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>
最佳答案
使用th:remove="tag"
。 (documentation)
fragment / 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>
关于thymeleaf - 使用Thymeleaf模板而不包含片段定义元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22023890/