我是 Thymeleaf 初学者。 我从一个通用布局页面开始:
fragments/layout.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="headerFragment">
<title>Template title</title>
<!-- metas, link and scripts -->
</head>
<body>
<div class="container">
Some text
</div>
</body>
</html>
和内容页面:
page.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/layout :: headerFragment">
<title>Page title</title>
<!-- metas, link and scripts -->
</head>
<body>
<div class="container">
Some text
</div>
</body>
</html>
当我渲染页面时,标题始终来自模板,而不是页面。 Thymeleaf 中是否可以有共同的元数据、脚本和样式(在 HEAD 标签中),但有一个每页标题?
最佳答案
我也遇到了这个问题(感谢nmy引用documentation!)这是我注意到的以及我如何在我的应用程序中解决它:
文档中需要注意的事项:
th:include
和th:replace
之间的区别- 通过 domselector 而不是通过
th:fragment
引用片段 - Thymeleaf 提供了“
this
”选项来查找选择器
考虑到这三件事,您可以执行以下操作:
片段/layout.html:
<head th:fragment="headerFragment">
<title th:include=":: #pageTitle">Layout Generic Title< /title>
<!-- metas, link and scripts -->
</head>
页面.html
<head th:include="fragments/layout :: headerFragment">
<title id="pageTitle">Page title</title>
<!-- other elements you want to reference in your layout -->
</head>
希望这有帮助!
关于thymeleaf - Thymeleaf 中的标题和标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31451830/