我有一个包含在布局中的 html 片段。此布局存在于所有页面中。现在我只有一个页面,我想从中排除这个片段。
布局页面:
<div th:replace="~{chat/head.html :: head}"></div>
<div th:replace="~{chat/footer.html :: footer}"></div>
所有页面:包括布局。 最后几页:包括没有的布局:
<div th:replace="~{chat/footer.html :: footer}"></div>
最佳答案
一种解决方案是在布局页面中添加一个标志:
<div th:replace="~{chat/head.html :: head}"></div>
<th:block th:if="${!#bools.isTrue(hideFooter)}">
<div th:replace="~{chat/footer.html :: footer}"></div>
</th:block>
然后在服务器上:
@GetMapping("/thatOnePageThatDoesntNeedAFooter")
public String getThatOnePage(Model model) {
model.addAttribute("hideFooter", true);
return "thatOnePageThatDoesntNeedAFooter";
}
逻辑可能看起来有点奇怪,但这将减少在除此异常(exception)情况之外的所有其他情况下向模型添加变量的需要。
请注意,您需要将 th:if
放在 th:replace
block 之外。如果将它们放在同一行,th:replace
将优先于 th:if
。
关于html - 如何使用 thymeleaf 从一页中删除 html 片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55099009/