【问题标题】:Grails GSP - cannot call body with parametersGrails GSP - 无法使用参数调用正文
【发布时间】:2012-10-21 22:21:06
【问题描述】:

我已经定义了一个这样的标签库:

class FooTagLib {
    static namespace = "foo"

    def bar = { attrs, body ->
        out << render(template: "/taglib/foo/bar", model: [body: body])
    }
}

body 闭包有两个参数,baz 和 qux,为什么我的 /taglib/foo/_bar.gsp 不能这样做:

${body(baz: 'Hello', qux: 'world!')}

?

这就是我在 gsp 视图中使用此标签的方式:

<foo:bar>
    ${baz} ${qux}
</foo:bar

打印body的内容,但是参数都是null:

null null

这是一个错误还是我做错了什么?

【问题讨论】:

    标签: grails gsp taglib


    【解决方案1】:

    在 taglib 中你从不指定任何参数,它不会自动完成,因为 taglib 不知道映射键的名称。您必须在模型中指定映射键和值。

    class FooTagLib {
        static namespace = "foo"
    
        def bar = { attrs, body ->
            def s = body()
            def tokens = s.tokenize()
            out << render(template: "/taglib/foo/bar", model: [body: [baz:tokens[0], qux:tokens[1]] ])
        }
    }
    

    也许 body-tokenize 并不是你真正应该做的,但它只是为了让事情变得清晰。

    使用attrs 会更容易,而不是使用参数构建主体闭包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多