两种 thymeleaf - layout

th:fragment

公共head

layout/layout.html

<head th:fragment="layout_head(title, links)">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title th:replace="${title}">标题</title>
    <link rel="stylesheet" href="xxx.css">
    <th:block th:replace="${links}"/>
</head>
<body class="layout-body" th:fragment="layout_body(tags)">
    <header>头部</header>
    <th:block th:replace="${tags}"/>
    <footer>尾部</footer>
</body>

index.html
不引入css

<head th:replace="layout/layout:: layout_head(~{::title}, ~{})">
    <title>控制台</title>
</head>

引入css

<head th:replace="layout/layout :: layout_head(~{::title}, ~{::link})">
    <title>控制台</title>
    <link rel="stylesheet" href="1.css">
    <link rel="stylesheet" href="2.css">
</head>
<body th:replace="layout/layout :: layout_body(~{::#layout_main})">
    <div id="layout_main">内容</div>
</body>
公共标签

layout/layout.html

<!-- 第一种 声明ID -->
<div id="idName">内容</div>
<!-- 第二种 th:fragment -->
<div th:fragment="name">内容</div>

index.html

<div th:replace="layout/layout :: #idName"></div>
<div th:replace="layout/layout :: name"></div>

th:include(template :: selector):加载模板内容(不保留模板节点)
th:replace(template :: selector):加载模板标签,替换掉当前标签
th:insert(template :: selector):加载模板标签到当前标签内容下

layout:fragment

layout/layout.html

<!DOCTYPE html>
<html xmlns:layout="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>标题</title>
    <link rel="stylesheet" href="xxx.css">
</head>
<body class="layout-body">
    <header>头部</header>
    <main layout:fragment="content"></main>
    <footer>尾部</footer>
</body>
</html>

index.html

<!DOCTYPE html>
<html xmlns:layout="http://www.w3.org/1999/xhtml" layout:decorator="layout/layout">
<head>
    <title>index</title>
    <link rel="stylesheet" href="xxx2.css">
</head>
<body>
    <div layout:fragment="content">
        <h1>Hello World!!!</h1>
    </div>
</body>
</html>

index.html会覆盖layout.html的head的title标签,head其他标签会追加,index.html的body同一标签的属性会覆盖layout.html的属性

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值