thymeleaf 引入模板


thymeleaf 引入模板

 

 

*********************

模板引入表达式


~{templateName :: fragement} ==》 template :: fragment(两者等效)

 

templateName:模板名称,如 index.html在默认目录templates下,若要引入index.html中的片段,则需将templateName设置为index

fragment:th:fragment定义的名称,也可以是 juqery 选择器(#id、div、.class等)

 

说明:

templateName缺省(::fragment)引入本地模版

fragmentment缺省(templateName)将templateName全部引入

 

 

*********************

相关标签

 

th:fragment:定义模板片段

    <footer th:fragment="copy">
      &copy; 2011 The Good Thymes Virtual Grocery
    </footer>

 

th:insert:插入模板片段,模版片段作为当前标签的子标签插入

<div th:insert="~{footer :: copy}"></div>
<div th:insert="footer :: copy"></div>    //两者等同


************
插入结果

  <div>
    <footer>
      &copy; 2011 The Good Thymes Virtual Grocery
    </footer>
  </div>

 

th:replace:使用模板片段替换当前标签

<div th:replace="~{footer :: copy}"></div>
<div th:replace="footer :: copy"></div>       //两者等同


***********
插入结果

  <footer>
    &copy; 2011 The Good Thymes Virtual Grocery
  </footer>

 

th:include :从3.0 开始已经不推荐使用

 


 

*********************

示例

 

commons.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:fragment="header" th:align="center">
    <strong style="color: coral">欢迎,瓜田李下</strong>
</div>

<div id="footer" th:align="center">
    <strong style="color: deeppink">版权所有,切莫侵权</strong>
</div>
</body>
</html>

 

index.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:align="center">
    <strong>
        <span th:text="引入commons模板" style="color: purple"></span>
    </strong>
    <div th:insert="commons :: header"></div>
    <div th:replace="commons :: #footer"></div><br>

    <strong>
        <span th:text="引入本地模版" style="color: purple"></span>
    </strong>
    <div th:replace=":: '#1'"></div>

    <div id="1">
        <strong style="color: greenyellow">hello world</strong>
    </div>
</div><br>

<div th:align="center">
    <strong>
        <span th:text="引入全部commons模版" style="color: purple"></span>
    </strong>
    <div th:replace="commons"></div>
</div>
</body>
</html>

 

 

*********************

使用测试

 

                                             

 

               

说明:引入全部模版时,head信息也会引入

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值