SpringBoot-thymeleaf公共页面元素抽取(十二)

一个同类型的网页中有着大量的重复的内容,我们需要把重复的内容抽取出来,这样就会显得网页不是那么臃肿.

{templatename::fragmentname}:模板名::片段名

1、抽取公共片段

<nav class="xxx" th:fragment="topbar">

使用标签 th:fragment="xxx"

2、引入公共片段

<!--映入抽取的topbar-->
<!--模板名:会使用thymeleaf的前后坠配置规则进行解析-->
     <div th:replace="dashboard::topbar"></div>

{templatename::selector}:模板名::选择器

1、抽取公共片段

<nav class="xxx" id="xxx">

2、引入公共片段

<div th:replace="~{dashboard::#sidebar}"></div>

三种引用属性的区别

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

引入方式
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>

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

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

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

三种引入公共片段的th属性:

th:insert:将公共片段整个插入到声明引入的元素中

th:replace:将声明引入的元素替换为公共片段

th:include:将被引入的片段的内容包含进这个标签中

注意点:

insert的公共片段在div标签中,
如果使用th:insert等属性进行引入,可以不用写~{},
行内写法可以加上:[[~{}]];[(~{})];

 

 

posted @ 2019-08-15 15:59  七月的风没有雨  阅读(998)  评论(0编辑  收藏  举报