【问题标题】:Why th:include is no longer recommended since Thymeleaf 3.0?为什么自 Thymeleaf 3.0 起不再推荐 th:include?
【发布时间】:2017-12-24 14:33:43
【问题描述】:

http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#template-layout docs 说:

上面的代码定义了一个名为 copy 的片段,我们可以使用 th:insert 或 th:replace 属性之一轻松将其包含在主页中(还有 th:include,尽管不再推荐使用它,因为Thymeleaf 3.0)。

我个人发现th:insert 是实现模板的唯一可能方式。

那是什么意思? th:insert 已弃用或将在未来版本中删除?或者这是不好的做法?

是什么让th:insert 名声不好?

【问题讨论】:

    标签: thymeleaf


    【解决方案1】:

    我认为这是对th:include 之前提供的内容进行概括的情况...

    Thymeleaf 3.0 引入了一种新类型的表达式作为 通用 Thymeleaf 标准表达系统:片段表达。

    它们看起来像这样:~{commons::footer} 是的,它们非常 类似于可以在 th:replace 和 th:include (现在 th:insert) 很久以前......因为他们完全使用 该语法,但已通用化,因此它现在可以用于其他 范围。

    Fragment Expressions

    Thymeleaf 文档中没有任何内容表明不应使用 th:include,也不建议在未来的版本中删除 th:include

    【讨论】:

    • 在文档中不再推荐它,并且 OP 提供了“不再推荐”的引用以及源链接。
    【解决方案2】:

    https://github.com/thymeleaf/thymeleaf/issues/625 中,我得到响应,从 AttoParser v2.0.3 和相应的 Thymeleaf v3.0.4 content() 选择器开始可以使用,th:replace 使 th:include 多余:

    <div th:replace="thymeleaf/layout :: tmpl(~{::body/content()})">
        XXX
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 2017-02-21
      • 2014-09-14
      • 2016-02-23
      相关资源
      最近更新 更多