templates - 如果来自片段,如何向 thymeleaf head 标签添加其他内容?

标签 templates spring-boot thymeleaf

在 Django 我可以这样做:

 <head>
{% block content %}

     (....) ---------> content from a partial template goes here.

{% endblock %}

     (....) ---------> I can add aditional stuff here if I need to.

<head>

我喜欢的地方。 (例如:在所有模板中使用相同的头部,但一个页面需要额外的 css 文件或脚本)

我知道在 thymeleaf 中,我需要将整个标签定义为一个片段,我无法再向其中添加任何其他内容并且需要按原样使用。

我的问题是我将如何在 thymeleaf 中实现上述示例?

最佳答案

在资源/模板下创建一个名为片段的文件夹。创建文件 fragment.html 后,在你的脑海中:

 <head>

     <div th:replace="fragments/fragment :: fragment"></div>


     (....) ---------> you can add aditional stuff here if you need to.

<head>

在你的 fragment.html 中:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
  <div th:fragment="fragment" th:remove="tag">
    (....) ---------> content from a partial template goes here.
  </div>
</body>
</html>

th:replace 实际上将用片段的主机标记替换

th:remove="标签"删除了包含的片段的容器 div

结果你应该得到:
 <head>

     (....) ---------> content from a partial template goes here.

     (....) ---------> you can add aditional stuff here if you need

<head>

关于templates - 如果来自片段,如何向 thymeleaf head 标签添加其他内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45739061/

相关文章:

c++ - 从 Typelist 子集创建函数指针

c++ - 隔离包含类型时的 std::variant 模板推导

Spring + Thymeleaf 形式的转换器

javascript - thymeleaf : create URL with parameters

c++ - 有没有办法从整数获取模板类型?

c++ - 具有 constexpr 函数的模板作为参数返回

java - 将 Spring Boot 应用程序作为 systemd 服务运行时未创建 pid 文件

spring-boot - SQS 监听器@Headers 获取正文内容而不是消息属性

java - Jhipster 和 Angular 过滤器

java - Spring Boot 和 Ajax 响应