Grails GSP - cannot call body with parameters

好久不见. 提交于 2019-12-10 11:53:17

问题


I have defined a taglib like this:

class FooTagLib {
    static namespace = "foo"

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

The body closure takes two parameters, baz and qux, why can't I do this in my /taglib/foo/_bar.gsp:

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

?

This is how I use this tag in my gsp views:

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

It prints the content of the body, but the parameters are all null:

null null

Is this a bug or is there something I am doing wrong?


回答1:


Inside the taglib you never specify any params, it is not done automatically, because the taglib does not know the names of the map keys. You must specify the map keys and values in the model.

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]] ])
    }
}

Maybe the body-tokenize is not really what you should be doing, but it was just to make things clear.

It would be easier for you to use attrs, instead of building the body closure with params.



来源:https://stackoverflow.com/questions/13002771/grails-gsp-cannot-call-body-with-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!