thymeleaf th:insert th:replace th:include

thymeleaf th:insert th:replace th:include

1.抽取公共片段

抽取公共片段: th:fragment="片段名"

test.html
<test th:fragment="test">
    <p>thymeleaf</p>
</test>

2.引入公共片段

引入公共片段:th:insert/replace/include="~{模板名(html文件名)::片段名}"
~{}可省略
行内写法:[[~{}]],[(~{})] ~{}不可省略

    <div th:insert="~{test::test}"></div>
    <div th:replace="~{test::test}"></div>
    <div th:include="~{test::test}"></div>

3.默认效果

th:insert:将公共片段,整个插入到原有标签内
th:replace:用公共片段代替原有标签
th:include:将公共片段的内容,放入原有标签内
原有标签中的内容会被替代

   <!--th:insert:将公共片段,整个插入到原有标签内 -->
    <div>
        <test>
             <p>thymeleaf</p>
        </test>
    </div>

  <!--th:replace:用公共片段代替原有标签-->
    <test>
        <p>thymeleaf</p>
    </test>

   <!--th:include:将公共片段的内容,放入原有标签内-->
    <div>
        <p>thymeleaf</p>
    </div>
发布了47 篇原创文章 · 获赞 7 · 访问量 2354

猜你喜欢

转载自blog.csdn.net/qq_43616898/article/details/104052655
th