Thymeleaf的模板使用介绍

参考网址: https://blog.csdn.net/hry2015/article/details/73476973

先定义一个html文件, 如下:

复制代码
文件路径: templates/template/footer.html 
内容如下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8" />
<body>
    <span th:fragment="copy"> 2017 hry loaded by fragment=copy</span>
    <span id="copy-section"> 2017 hry loaded by id=copy-section</span>
<span th:fragment="frag(month, date)"> <span th:text="'welcome hry come in ' + ${month} + '-' + ${date}"></span></span> </body> </html>
复制代码
复制代码
fragment语法

加载语法如下: templatename::selector:”::” 前面是模板文件名,后面是选择器 ::selector:只写选择器,这里指fragment名称,则加载本页面对应的fragment templatename:只写模板文件名,则加载整个页面

example:
<!-- 语法说明 "::"前面是模板文件名,后面是选择器 -->
<div th:include="
templates/template/footer::copy"></div>

<!-- 只写选择器,这里指fragment名称,则加载本页面对应的fragment -->
<div th:include="::#
copy-section"></div>

<!-- 只写模板文件名,则加载整个页面 -->
<div th:include="
templates/template/footer"></div>
复制代码
复制代码
    <!--  语法说明"::"前面是模板文件名,后面是选择器 -->
    <div> 2017 hry loaded by fragment=copy</div>
<!-- 只写选择器,这里指fragment名称,则加载本页面对应的fragment --> <div>2017 hry loaded by id=copy-section</div>
<!-- 只写模板文件名,则加载整个页面 -->
    <div>
        <html>
        <meta charset="UTF-8" />
        <body>
            <spanth:fragment="copy"> 2017 hry loaded by fragment=copy</span> 
       <span id="copy-section"> 2017 hry loaded by id=copy-section</span>
<span th:fragment="frag(month, date)"> <span th:text="'welcome hry come in ' + ${month} + '-' + ${date}"></span></span>
        </body> 
</html>
</
div>
复制代码
<!-- 这里加载”th:fragment 定义用于加载的块“ -->
<div th:include="templates/template/footer::copy"></div> 

<!-- 这里加载”id=copy-section“的节点 -->
<div th:include="templates/template/footer::#copy-section"></div>

结果:
<!-- 这里加载”th:fragment 定义用于加载的块“ -->
<div> 2017 hry loaded by fragment=copy</div>

<!-- 这里加载”id=copy-section“的节点 -->
<div> 2017 hry loaded by id=copy-section</div>
复制代码
th:include 和 th:replace 区别
<span th:fragment="copy">2017 hry loaded by fragment=copy</span>
th:include: 加载模板的内容: 读取加载节点的内容(不含节点名称),替换div内容
th:replace: 替换当前标签为模板中的标签,加载的节点会整个替换掉加载他的div

<!-- 加载模板的内容: 读取加载节点的内容(不含节点名称),替换<div>的内容 -->
<div th:include="templates/template/footer::copy">1</div>
<!-- 替换当前标签为模板中的标签: 加载的节点会整个替换掉加载他的<div>  -->
<div th:replace="templates/template/footer::copy">2</div>

结果:
<!-- 加载模板的内容: 读取加载节点的内容(不含节点名称),替换<div>的内容 -->
<div> 2017 hry loaded by fragment=copy</div>

<!-- 替换当前标签为模板中的标签: 加载的节点会整个替换掉加载他的<div>  -->
<span> 2017 hry loaded by fragment=copy</span>
复制代码
复制代码
 参数化模板配置

<span th:fragment="frag(month, date)"> <span th:text="'welcome hry come in ' + ${month} + '-' + ${date}"></span></span>

${month} 和 ${date}是从后台传过来的
<div th:include="templates/template/footer::frag(${month},${date})">config</div>

结果:
<div> <span>welcome hry come in 6-19</span></div>
复制代码

 

posted on   努力做一个伪程序员  阅读(1306)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示