【问题标题】:Grails GSP outer templates with inner content?具有内部内容的 Grails GSP 外部模板?
【发布时间】:2013-03-26 19:59:33
【问题描述】:

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 下找不到等价物。

【问题讨论】:

  • 我不确定你在问什么。模板可以渲染模板。
  • 你能详细说明你的问题吗?
  • @James McMahon,您的问题对于您尝试进行的明显重构来说太具体了。我们首先需要知道 tmpl tagLib 做了什么。
  • 我试图让问题更清楚,很抱歉造成混乱。

标签: grails grails-2.0 gsp


【解决方案1】:

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

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

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

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

body()用于渲染标签正文内容。

(示例复制自官方grails documentation

【讨论】:

  • 啊,我支持我可以制作一个标签库来渲染另一个模板并传入内部内容,看起来有点沉重,但我仍在弄清楚所有这些 grails / gsp 的东西
【解决方案2】:

好吧,我要找的是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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    相关资源
    最近更新 更多