【问题标题】:Insert template without overriding childs in Thymeleaf (default head template)在 Thymeleaf 中插入模板而不覆盖子项(默认头模板)
【发布时间】:2020-06-01 21:53:05
【问题描述】:

我习惯了th:insertth:replace,但我需要一种方法来在标题中包含一个片段,而不会覆盖标题中的其他标签。

我试过这个,但 home.css 头部被完全覆盖:

home.html:

<head th:include="fragments/head :: genericHead">
    <link rel="stylesheet" href="home.css">
</head>

头部模板:

<head th:fragment="genericHead">
    <link rel="stylesheet" href="common.css">
</head>

我想要一些方法来执行 th:add 或类似操作,以便将两个链接都作为 head 的子级,并导致如下结果:

<head>
    <link rel="stylesheet" href="common.css">
    <link rel="stylesheet" href="home.css">
</head>

【问题讨论】:

    标签: html spring spring-boot spring-mvc thymeleaf


    【解决方案1】:

    您可以将th:blockth:include / th:replace 一起使用

    你的home.html 看起来像:

    <head>
        <th:block th:include="fragments/head :: genericHead"></th:block>
        <link rel="stylesheet" href="../static/home.css">
    </head>
    

    片段看起来完全一样:

    <head th:fragment="genericHead">
        <link rel="stylesheet" href="common.css">
    </head >
    

    th:block 将执行include 然后它会消失(所以它可以是include 也可以是replace,毕竟没关系)给我们留下片段的内容标记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-21
      • 2012-03-15
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2020-08-23
      相关资源
      最近更新 更多