Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Thymeleaf 3(非)建筑布局顺序?_Spring_Thymeleaf - Fatal编程技术网

Spring Thymeleaf 3(非)建筑布局顺序?

Spring Thymeleaf 3(非)建筑布局顺序?,spring,thymeleaf,Spring,Thymeleaf,我知道关于Thymeleaf()的布局方言的存在,但在跳到Thymeleaf 3之前的方言之前,我想探索新的片段表达式(在TL3中介绍) 因此,根据docs,我可以定义base.html,其工作方式与布局方言允许管理片段的方式几乎相同 base.html: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head th:fragment="base(title, links, conte

我知道关于Thymeleaf()的布局方言的存在,但在跳到Thymeleaf 3之前的方言之前,我想探索新的片段表达式(在TL3中介绍)

因此,根据docs,我可以定义base.html,其工作方式与布局方言允许管理片段的方式几乎相同

base.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head th:fragment="base(title, links, content)">
        <title th:replace="${title}"></title>
        <link rel="stylesheet" type="text/css" media="all" href="/css/main.css" />
        <th:block th:replace="${links}"></th:block>
    </head>
    <body>
        <header th:replace="~{header :: header}"></header>
        <div th:replace="${content}"></div>
        <footer th:replace="~{footer::footer}"></footer>
    </body>
</html>

…但当我在my home.html中使用此选项时:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head th:replace="base::base(~{::title}, ~{::link}, ~{::content})">
        <title th:text="#{home.title}"></title>

        <link rel="stylesheet" th:href="test">
        <link rel="stylesheet" th:href="test1">
        <link rel="stylesheet" th:href="test2">
    </head>
    <body>
        <div th:fragment="content">
            <span>TEST CONTENT</span>
        </div>
    </body>
</html>

测试内容
…它的行为类似于home.html只计算自身和传递给base.html的参数,因为这是产品:

<!DOCTYPE html>
<html>
    <head>
    <title>My home page</title>

    <link rel="stylesheet" type="text/css" media="all" href="/css/main.css" />
    <link rel="stylesheet" href="test"><link rel="stylesheet" href="test1"><link rel="stylesheet" href="test2">
    </head>
    <body>
        <div>
            <span>TEST CONTENT</span>
        </div>
    </body>
</html>

我的主页
测试内容
如您所见,标题在home.html中计算并传递到base.html,我在home.html中提供的3个链接也是如此。内容也会被传递并放置在适当的位置。少了什么?不是base.html片段参数的所有内容。Thymeleaf忽略对页眉和页脚的求值,只删除它们

请允许我注意,如果我将页眉/页脚放在home.html中的内容中,它们将按照应该的方式进行计算——从带有选择器页眉的header.html开始:“~{header::header}”


我是否遗漏了一些关键的东西,比如整个事情应该如何运作?如果不能自我评估并且需要从“子文件(调用方)”传递所有内容,那么能够定义可用作布局的片段有什么意义呢?

在我看来,您将片段和替换混合在一起了。更换头部效果良好,因为您正确声明了它。但是为什么在base.html中声明
th:replace
属性
?它不在片段中,也不是参数,因此必须从某处替换它

据我所知,您希望base.html中有片段,home.html中有替换。然后制作页眉和页脚片段,并在home.html中声明相应的
th:replace
标记

<div th:replace="${content}"></div>

这也不起作用,因为它不在
th:fragment
标记内。
底线是:修复标记的层次结构和替换逻辑。

就像Xaltotun指出的那样-我需要修复我的东西

我已经在标题中放置了base(args…),所以我传递的片段在身体中不可见,所以一切都失败了

让我添加一些代码:

Layout.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:fragment="layout(content, title, meta)">
<head>
<title th:text="${title}">Default Title</title>
<link rel="stylesheet" type="text/css" media="all" href="/css/style.css" />
<th:block th:replace="${meta}" />
</head>
<body>
    <div class="viewport">
        <header class="website-header">
            ...
        </header>

        <div th:replace="${content}"></div>

        <th:block th:replace="staticContentTemplate :: testFragment" />

        <footer class="website-footer">
            ...
        </footer>
    </div>
</body>
</html>

默认标题
...
...
Home.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="layout::layout(~{::content}, #{home.title}, ~{::meta})">
<head>
<th:block th:fragment="meta">
    <link rel="stylesheet" th:href="test">
</th:block>
</head>
<body>
    <div th:fragment="content">
        <span>TEST CONTENT</span>
    </div>
</body>
</html>

测试内容
结果如预期:

  • html提供标题、内容和元数据
  • Layout.html提供了所有其他内容,还从不同的源代码复制了其他模板
    staticContentTemplate::testFragment
  • 给其他人的提示:请注意,我在布局中按以下顺序放置了参数-内容始终存在,标题可以是可选的,元数据可能不需要-这允许我们仅使用内容调用布局:
    layout(~{::content})
    (更短=更好)

多亏了你的帖子,我意识到我把base(args…)放在了中而不是中。不幸的是,我会在几天后再回来汇报更多的事情。谢谢(这可能会解决我的问题)。