iaf*_*rek 7 layout dialect thymeleaf
我正在学习本教程: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>
Run Code Online (Sandbox Code Playgroud)
th:replace在上面的示例中,页脚和标题被标记替换,而在布局文件中<head>有<title>标记.
基本上,我想用整个<head>标签替换th:replace.因此,我有:
我的布局文件:
<!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>
Run Code Online (Sandbox Code Playgroud)
我的内容文件:
<!DOCTYPE html>
<html layout:decorator="/html/layouts/layout">
<head>
<title>My content title</title>
</head>
<body>
<div layout:fragment="content">
...
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
最后我的/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>
Run Code Online (Sandbox Code Playgroud)
内容没问题.页脚和头部按预期包含(替换)在文件中,但页面标题为空白!
我明白了:
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title></title>
...
Run Code Online (Sandbox Code Playgroud)
怎么了?
iaf*_*rek 31
最后,我找到了实现我想要的方法.
在布局文件<title>标签必须保持.我用<object>tag 分组的所有其他标签,并注释如下:
<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>
Run Code Online (Sandbox Code Playgroud)
在我的html/components/head.htm文件中,我不得不删除<title>标记,以便在包含后不会重复.
<head th:fragment="head">
<meta charset="utf-8" />
<!-- NO TITLE TAG HERE -->
...
</head>
Run Code Online (Sandbox Code Playgroud)
这样头部片段就包含在<object>标签中,感谢th:remove="tag" <object>标签被删除,我的最终HTML输出是:
<head>
<title>My content title</title>
<meta charset="utf-8" />
<!-- NO TITLE TAG HERE -->
...
</head>
Run Code Online (Sandbox Code Playgroud)
显然,一旦我开始工作,我也删除了NO TITLE TAG HERE消息.
小智 7
我想我找到一个稍微不那么冗长的方式来使用th:replace和th:fragment在一起,例如,包括常见的<head>元数据和静态资源包括在你的页面.
把th:remove="tag"在片段定义,所以您不必重复th:remove="tag"每次包括它.
fragment_head.html
<thymeleaf xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
th:fragment="head" th:remove="tag">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" th:href="@{/css/vendor/bootstrap.min.css}"/>
</thymeleaf>
Run Code Online (Sandbox Code Playgroud)
的mypage.html
<head>
<thymeleaf th:replace="fragment_head :: head" />
</head>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18884 次 |
| 最近记录: |