Thymeleaf学习笔记(三)

Layout布局

1 定义fragment
语法: th:fragement
示例:

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

fragmeng使用示例:

<body>
  ...
  <div th:insert="~{footer :: copy}"></div>
  <div th:insert="footer :: copy"></div>
</body>

下面是一个动态的模板fragment示例:

<div th:insert="footer :: (${user.isAdmin}? #{footer.admin} : #{footer.normaluser})"></div>

上述示例中,功能强大,可以实现动态的fragment替换。

th:replace vs th:insert
insert方法会在fragment外层创建一个div,而replace则不会。
示例如下:

<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>

输出的结果信息如下:

  <div>
    <footer>
      &copy; 2011 The Good Thymes Virtual Grocery
    </footer>
  </div>
  <!-- replace output -->
  <footer>
    &copy; 2011 The Good Thymes Virtual Grocery
  </footer>

comments

示例1:
示例2,中间的代码将被parser忽略.

<!--/*--> 
  <div>
     you can see me only before Thymeleaf processes me!
  </div>
<!--*/-->

示例3:

<!--/*/ <th:block th:each="user : ${users}"> /*/-->

中间的内容在浏览器中展示之时,将自动被移除掉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值