胸腺布局方言和头部的th:replace导致标题为空白 [英] Thymeleaf layout dialect and th:replace in head causes title to be blank
问题描述
我正在关注本教程: http://www.thymeleaf.org/doc/layouts.html(进入Thymeleaf布局方言部分). 在其中可以找到示例:
I'm following this tutorial: http://www.thymeleaf.org/doc/layouts.html (got to Thymeleaf Layout Dialect section). In there you can find an example:
<!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>
标记.
Footer and header are replaced by th:replace
tag in above example, while <head>
has <title>
tag in layout file.
基本上,我想将整个<head>
标记替换为th:replace
.
因此,我有:
Basically, I want to replace whole <head>
tag with th:replace
.
Therefore, I have:
我的布局文件:
<!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文件:
And finally my /html/components/head.htm file:
<!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>
内容还可以.文件中的页脚和页眉已包括在内(已替换),但页面标题为空白!
Content is alright. Footer and head are included (replaced) from files as expected but page title is blank!
我得到:
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title></title>
...
怎么了?
推荐答案
最后,我找到了一种实现所需目标的方法.
Finally, I've found a way to achieve what I wanted.
必须保留在布局文件<title>
标签中的 .我将所有其他标记与<object>
标记分组并按如下注释:
In layout file <title>
tag must stay. All other tags I grouped with <object>
tag and annotated it as follows:
<head>
<title layout:title-pattern="$CONTENT_TITLE">Layout Title will be replaced by Page Title!</title>
<object th:include="/html/components/head :: head" th:remove="tag" />
</head>
在我的html/components/head.htm文件中,我必须删除<title>
标记,因此在包含之后将不会重复该标记.
In my html/components/head.htm file I had to remove <title>
tag so it won't be duplicated after include.
<head th:fragment="head">
<meta charset="utf-8" />
<!-- NO TITLE TAG HERE -->
...
</head>
这样,在<object>
标记中包含了头片段,由于th:remove="tag"
<object>
标记被删除,我最终的HTML输出是:
This way head fragment is included in <object>
tag and thanks to th:remove="tag"
<object>
tag gets removed and my final HTML output is:
<head>
<title>My content title</title>
<meta charset="utf-8" />
<!-- NO TITLE TAG HERE -->
...
</head>
很明显,一旦它起作用,我也删除了TITLE TAG HERE消息.
Obviously, I removed NO TITLE TAG HERE message too, once I got it working.
这篇关于胸腺布局方言和头部的th:replace导致标题为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!