我是百里香初学者。
我从一个通用的布局页面开始:
片段/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>
和内容页面:
页面.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 标签中),但有一个per-page title?
我也遇到这个问题了(谢谢nmy https://stackoverflow.com/users/4165181/nmy供参考文档 http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#including-template-fragments!)这是我注意到的以及我如何在我的应用程序中解决它:
文档中需要注意的事项:
- 之间的差异
th:include
and 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>
希望这可以帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)