Grails GSP外部模板有内在内容吗?

时间:2013-03-26 19:59:33

标签: grails grails-2.0 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下找到一个等价物。

2 个答案:

答案 0 :(得分:1)

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

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

这定义了一个可以在gsp中使用的标记emoticon,如下所示:

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

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

(该示例是从官方grails documentation

复制而来的

答案 1 :(得分:1)

好的,我正在寻找的是Grails' SiteMesh layout support,它允许我以更加雄辩的方式定义常用的视图标记,然后是模板。

因此页眉和页脚内容可以放在布局

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

我认为它比页眉和页脚模板更清晰。

You can also nest layouts.