Grails GSP外部模板与内部内容



在Grails GSP中有没有办法替换下面的

<tmpl:/templates/header />
<!-- tmpl namespace call is equivalent to <g:render template="/templates/header" /> -->    
<!-- definition of inner content -->
<tmpl:/templates/footer />

使用外部模板?本质上,这是一种导入包装外部模板的方法,

<outertemplate:templatename>
<!-- header will be rendered from outer template -->
<!-- definition of inner content -->
<!-- footer will be rendered from outer template -->
</outertemplate:templatename>

和外部模板的定义是类似于

的东西
<!-- definition of header content -->
<!-- placeholder or attr for inner content -->
<!-- definition of footer content -->

将包装内容封装在单个模板中,而不是两个模板中。IIRC在JSF下有办法做到这一点,但我在GSP下找不到等效的

好的,所以我一直在寻找的是Grails的SiteMesh布局支持,它允许我以比模板更雄辩的方式定义常用的视图标记。

所以页眉和页脚的内容可以放在一个布局

<html>
    <head>
        <title><g:layoutTitle/></title>
        <g:layoutHead />
    </head>
    <body>
        <!-- HEADER CONTENT DEFINED HERE (or for stuff in head in the head above -->
        <g:layoutBody />
        <!-- FOOTER CONTENT DEFINED HERE -->
    </body>
</html>

然后使用布局

<html>
    <head>
        <title>An Example Page</title>
        <meta name="layout" content="main" />
    </head>
    <body>This is my content!</body>
</html>

我认为它比页眉和页脚模板干净得多。

你也可以嵌套布局

您可以使用标记库创建类似的内容。

class SimpleTagLib {
    def emoticon = { attrs, body ->
       out << body() << (attrs.happy == 'true' ? " :-)" : " :-(")
    }
}

这定义了一个标签emoticon,可以像这样在gsp中使用:

<g:emoticon happy="true">Hi John</g:emoticon>

body()用于呈现标签正文内容。

(该示例复制自grails官方文档)

最新更新