Thymeleaf 模板 - 有没有办法装饰模板而不是包含模板片段? [英] Thymeleaf templates - Is there a way to decorate a template instead of including a template fragment?

查看:26
本文介绍了Thymeleaf 模板 - 有没有办法装饰模板而不是包含模板片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用 Thymeleaf,我需要对模板进行说明.如果我正确理解了文档,我可以在我的页面中包含一个模板——或者只是它的一个片段.例如,我可以这样写:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><head th:include="模板/布局::标题"><身体>你好,世界<div th:include="模板/布局::页脚"></div></html>

但实际上我想要的是使用模板的相反方式:我想在我的模板中包含页面inside,而不是在页面中包含模板片段,类似这样:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><头>...<身体><div id="my-template-header">...</div><div id="内容"><!-- 在此包含用户访问的当前页面的内容-->???

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

换句话说,有没有一种方法可以等效于 Sitemesh 装饰器标签 在百里香叶?

谢谢

解决方案

好的,正如 Sotirios Delimanolis 所说,Thymeleaf 不支持这种使用模板的方式,或者我应该说Hierarchical layouts",正如 Daniel Fernandez 在此主题.

由于 sitemesh 和 Thymeleaf 是兼容的,看来我必须同时使用这两种解决方案.可惜了.

正如 DennisJaamann 在评论中所建议的,我最终使用了 Thymeleaf Layout Dialect,这是一种视图方言提供了我正在寻找的功能.

工作代码:

首先我添加LayoutDialect 类:

@Bean公共 ServletContextTemplateResolver templateResolver() {ServletContextTemplateResolver 解析器 = new ServletContextTemplateResolver();resolver.setPrefix("/WEB-INF/views/");resolver.setSuffix(".html");//注意,选择HTML5作为模板模式.resolver.setTemplateMode("HTML5");resolver.setCacheable(false);返回解析器;}

然后,我创建模板(例如 templates/layout.html),并添加 layout:fragment 信息到我想放置内容的位置当前页面:

<头>...<身体><div id="my-template-header">...</div><div id="the-content" layout:fragment="content"><!-- 在此包含用户访问的当前页面的内容-->

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

并且页面将引用具有 layout:decorator:

属性的模板

<身体><div layout:fragment="content">你好,世界

</html>

I am working with Thymeleaf for the first time, and I need a clarification about the templates. If I correctly understand the documentation, I can include a template - or just a fragment of it - in my page. So for example, I can write something like that:

<!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>

But what I want is in fact the opposite way of using the template : instead of including template fragment in the page, I want to include the page inside my template, something like that:

<!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>

In others words, is there a way to have an equivalent of the Sitemesh decorators tags in Thymeleaf?

Thanks

解决方案

Ok, as stated by Sotirios Delimanolis, Thymeleaf does not support that way of using template, or should I say "Hierarchical layouts", as explained by Daniel Fernandez in this thread.

As sitemesh and Thymeleaf are compatible, it seems that I have to use both solutions. Too bad.

Edit: As suggested by DennisJaamann in a comment, I finally used Thymeleaf Layout Dialect, a view dialect that provides the feature I was looking for.

The working code:

First I add the LayoutDialect class:

@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;
}

Then, I create the template (for ex. templates/layout.html), and add the layout:fragment information where I want to put the content of the current page:

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

and the page will refers to the template with the attribute layout:decorator:

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

这篇关于Thymeleaf 模板 - 有没有办法装饰模板而不是包含模板片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆