Thymeleaf学习记录(7)--页面引入/片段引入

时间:2023-03-09 17:22:36
Thymeleaf学习记录(7)--页面引入/片段引入

1.为页面添加footer

Templates文件夹下新建HTML文件:

 <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"> <body> <div th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</div> </body> </html>

在主文件添加

 <div th:include="footer :: copy"></div>

即可。

运行结果如下:

Thymeleaf学习记录(7)--页面引入/片段引入

2.th:includeth:replace之间的区别

th:include将片段的内容包含在其主机标签中,但th:replace实际上将用片段替换主机标签

3.可参数化的片段签名

前台插入代码:

 <div th:fragment="frag (a,b)">
<p th:text="hello + ${a} + ' - ' + ${b}">...</p>
</div> <div th:include="::frag (zhang,san)">...</div>
<div th:include="::frag (a=li,b=si)">...</div>

运行结果如下:

Thymeleaf学习记录(7)--页面引入/片段引入

3.文本内联

采用inline关键字可以将表达式嵌入文本。

以下两种方式等价:

 <h1>Hello : <b th:text="${user.name}">姓名</b></h1>
<p th:inline="text">Hello, [[${user.name}]]!</p>

运行结果:

Thymeleaf学习记录(7)--页面引入/片段引入

此外,还可以签入Js文件

 <script th:inline="javascript">
/*<![CDATA[*/
... var username = /*[[${session.user.name}]]*/ 'Sebastian'; ...
/*]]>*/
</script>