在 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/