【发布时间】:2023-04-04 19:51:02
【问题描述】:
我正在关注本教程:http://www.thymeleaf.org/doc/layouts.html(进入 Thymeleaf 布局方言部分)。 在那里你可以找到一个例子:
<!DOCTYPE html>
<html>
<head>
<!--/* Each token will be replaced by their respective titles in the resulting page. */-->
<title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Task List</title>
...
</head>
<body>
<!--/* Standard layout can be mixed with Layout Dialect */-->
<div th:replace="fragments/header :: header">
...
</div>
<div class="container">
<div layout:fragment="content">
...
</div>
<div th:replace="fragments/footer :: footer">© 2014 The Static Templates</div>
</div>
</body>
</html>
在上面的例子中,页脚和页眉被th:replace标签替换,而<head>在布局文件中有<title>标签。
基本上,我想用th:replace 替换整个<head> 标签。
因此,我有:
我的布局文件:
<!DOCTYPE html>
<html>
<head th:replace="/html/components/head :: head">
</head>
<body>
<div layout:fragment="content">
</div>
...
<div th:replace="/html/components/footer :: footer" />
</body>
<html>
我的内容文件:
<!DOCTYPE html>
<html layout:decorator="/html/layouts/layout">
<head>
<title>My content title</title>
</head>
<body>
<div layout:fragment="content">
...
</div>
</body>
</html>
最后是我的 /html/components/head.htm 文件:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head th:fragment="head">
<meta charset="utf-8" />
<title layout:title-pattern="$CONTENT_TITLE">Layout Title should be replaced by Content Title!</title>
...
</head>
<body>
</body>
</html>
内容没问题。文件中的页脚和标题按预期包含(替换),但页面标题为空白!
我明白了:
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title></title>
...
怎么了?
【问题讨论】:
-
如果我只是删除 并将头文件的内容直接放到 layout.htm 中,那么标题就可以了。但我不喜欢这个解决方案的丑陋。有什么方法可以移动整个头部并将其与布局分开?