首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Thymeleaf模板--是否有一种方法来装饰模板而不是包含模板片段?

Thymeleaf模板--是否有一种方法来装饰模板而不是包含模板片段?
EN

Stack Overflow用户
提问于 2013-09-19 14:05:32
回答 4查看 33.3K关注 0票数 39

我是第一次与Thymeleaf合作,我需要一个关于模板的澄清。如果我正确地理解了文档,我可以在我的页面中包含一个模板--或者仅仅是其中的一个片段。例如,我可以写这样的东西:

代码语言:javascript
复制
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <head th:include="template/layout :: header">
    </head>
    <body>
        Hello world
        <div th:include="template/layout :: footer"></div>
    </body>
</html>

但实际上,我想要的是使用模板的相反方式:我不想在页面中包含模板片段,而是希望将页面包含在 my中,类似于:

代码语言:javascript
复制
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    ...
</head>
<body>

    <div id="my-template-header">...</div>

    <div id="the-content">
        <!-- include here the content of the current page visited by the user -->
        ???
    </div>

    <div id="my-template-footer">...</div>
</body>

换句话说,在Thymeleaf中是否有一种类似于Sitemesh装饰标签的方法?

谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-09-19 14:55:49

好的,正如索蒂里奥斯·德里曼奥利所说,Thymeleaf不支持那种使用模板的方式,或者我应该说“分层布局”,正如Daniel在这条线中所解释的那样。

由于sitemesh和Thymeleaf兼容,我似乎必须同时使用这两种解决方案。太可惜了。

编辑:正如DennisJaamann在一条评论中所建议的,我最终使用了Thymeleaf方言,这是一种视图方言,提供了我想要的特性。

工作守则:

首先,我添加了LayoutDialect类:

代码语言:javascript
复制
@Bean
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".html");
    //NB, selecting HTML5 as the template mode.
    resolver.setTemplateMode("HTML5");
    resolver.setCacheable(false);
    return resolver;
}

然后,创建模板(用于ex )。( templates/layout.html),并在其中添加layout:fragment信息,以便将当前页面的内容放在其中:

代码语言:javascript
复制
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    ...
</head>
<body>
    <div id="my-template-header">...</div>

    <div id="the-content" layout:fragment="content">
        <!-- include here the content of the current page visited by the user -->
    </div>

    <div id="my-template-footer">...</div>
</body>

页面将引用带有属性layout:decorator的模板。

代码语言:javascript
复制
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="templates/layout">
<body>

    <div layout:fragment="content">
        Hello world
    </div>

</body>
</html>
票数 20
EN

Stack Overflow用户

发布于 2013-12-08 02:33:31

使用Thymeleaf 2.1,您可以编写类似的内容:

创建模板(用于ex )。模板/layout.html),并在html标记中添加th:fragment="page"信息,并使用th:include="this : content"信息定义内容区域:

代码语言:javascript
复制
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      th:fragment="page">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Test</title>
    </head>
    <body>
        layout page
        <div th:include="this :: content"/>
        layout footer
    </body>
</html>

现在,在html标记中创建包含此模板的页面,添加th:include=“template /layout ::page",并将您的主要内容放入带有th:include=的div中。

代码语言:javascript
复制
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      th:include="templates/layout :: page">
    <head>
        <title></title>
    </head>
    <body>
        <div th:fragment="content">
            my page content
        </div>
    </body>
</html>

在布局页面中,您可以使用 this (th:include=this ::content)或禁止此选项(th:include="::content")。我觉得好像是个小脸谱。

票数 67
EN

Stack Overflow用户

发布于 2014-03-29 02:04:38

你需要

代码语言:javascript
复制
<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
    <version>1.2.2</version>
</dependency>

并将其添加到您的SpringTemplateEngine中:

代码语言:javascript
复制
@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new LayoutDialect());

    return templateEngine;
}

如果现在在视图文件夹中创建一个名为template的文件夹。

视图/home.html.home.html

代码语言:javascript
复制
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  layout:decorator="template/layout">
 <body>
  <div layout:fragment="content">
    Hello world
  </div>
 </body>
</html>

视图/布局/layout.html

代码语言:javascript
复制
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org"
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
    <head>
      </head>
        <body>
          <div id="content" layout:fragment="content">
          </div>
</body>
</html>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18896915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档