Grails 标签未解析其主体中的变量
我认为在 grails 标签中解析变量时我遇到了一些困难。
在我的标签库中,我
def contentArea = {attrs, body ->
def domainObject = Class.forName("${attrs.contentType}", true, Thread.currentThread().contextClassLoader).newInstance()
def numberOfRows = !StringUtils.equals("${attrs.max}", "null")? new Integer("${attrs.max}") : new Integer("1");
def results = domainObject.getByContentAreaKey("${attrs.contentAreaKey}", numberOfRows)
out << g.render(
template: '/layouts/contentTag',
model: [contentAreaKey: attrs.contentAreaKey, results : results, contentNamespace: "${attrs.contentAreaKey}" + "_contentList", body:body()])
out << body()
}
在 _contentTag.gsp 中,布局是:
<b>In tag layout, </b>
<g:set var="${contentNamespace}" value="bobby"/>
contentNamespace = ${contentNamespace}<br/><!-- prints "minicontent_contentList" -->
minicontent_contentList = ${minicontent_contentList}<br/> <!-- prints "bobby" -->
在调用 gsp 中,调用标签:
<mynamespace:contentArea var="myVar" contentAreaKey="minicontent" contentType="com.my.test.MiniContentType">
<br/>Test Text<br/>
<b>in calling GSP,</b>
contentNamespace = ${contentNamespace}<br/><!-- prints nothing -->
minicontent_contentList = ${minicontent_contentList}<br/><!-- prints nothing -->
</mynamespace:contentArea>
contentNamespace 和 minicontent_contentList 在标签正文中未解析。变量是否可以解决?如果是这样,我该怎么办?
如果它对答案有帮助,我有一个包含许多小内容区域的页面,我希望能够通过不同的控制器进行管理。内容区域背后都有相似的数据(文本、链接、图形等),但布局会有所不同。我已经使用 sitemesh 布局来屏蔽页面,并且调用 gsp 代表这些 sitemesh 内容块之一。
我对 grails 和寻求帮助非常陌生,所以我非常愿意接受批评,但请保持温和。 :)
I think I'm having a little difficulty with when variables are parsed in grails tags.
In my tag library, I have
def contentArea = {attrs, body ->
def domainObject = Class.forName("${attrs.contentType}", true, Thread.currentThread().contextClassLoader).newInstance()
def numberOfRows = !StringUtils.equals("${attrs.max}", "null")? new Integer("${attrs.max}") : new Integer("1");
def results = domainObject.getByContentAreaKey("${attrs.contentAreaKey}", numberOfRows)
out << g.render(
template: '/layouts/contentTag',
model: [contentAreaKey: attrs.contentAreaKey, results : results, contentNamespace: "${attrs.contentAreaKey}" + "_contentList", body:body()])
out << body()
}
in _contentTag.gsp, the layout is:
<b>In tag layout, </b>
<g:set var="${contentNamespace}" value="bobby"/>
contentNamespace = ${contentNamespace}<br/><!-- prints "minicontent_contentList" -->
minicontent_contentList = ${minicontent_contentList}<br/> <!-- prints "bobby" -->
And in the calling gsp, the tag is called:
<mynamespace:contentArea var="myVar" contentAreaKey="minicontent" contentType="com.my.test.MiniContentType">
<br/>Test Text<br/>
<b>in calling GSP,</b>
contentNamespace = ${contentNamespace}<br/><!-- prints nothing -->
minicontent_contentList = ${minicontent_contentList}<br/><!-- prints nothing -->
</mynamespace:contentArea>
contentNamespace and minicontent_contentList are not resolved in the body of the tag. Is it possible for the variables to be resolved? If so, how should I do it?
In case it helps with the answer, I have a page with a number of small content areas that I want to be able to administer via a different controller. The content areas all have similar data behind them (text, link, graphic, etc), but the layouts will be different. I have used sitemesh layouts to block out the page, and the calling gsp represents one of those sitemesh content blocks.
I am very new to grails and to asking for help on SO, so I am perfectly willing to take criticism, but please be gentle. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
传入参数的
body
是一个Closure
,它将其方法和参数解析到声明它的位置,这里将是主 gsp。您可以尝试将body
的delegate
设置为标签库,并将resolveStrategy
设置为Closure.DELEGATE_FIRST
。这应该允许您解析contentNamespace
。解析
minicontent_contentlist
会更困难,因为我不确定如何将模板指定为委托。您可以尝试在标签库中定义变量并将其传递到模板模型,然后将minicontent_contentlist
值分配给该传递的对象,这可能会将该值更新回标签库代码中resolveStrategy
可以工作,假设它是通过引用传递的同一个对象。作为最后一个选项,您可以尝试在模板中的 gsp curlies (
${}
) 内分配delegate/resolveStrategy
,看看是否将模板对象分配给委托参数。
The
body
, as passed in parameter, is aClosure
, which will resolve its methods and parameters to the location where it was declared, which here will be the main gsp. You could try setting thedelegate
of thebody
to the tag library and set theresolveStrategy
toClosure.DELEGATE_FIRST
. This should allow you to resolvecontentNamespace
.To resolve
minicontent_contentlist
would be harder as I'm not sure how to specify the template as the delegate. You could try defining the variable in the tag tibrary and passing it in to the template model, then you assign theminicontent_contentlist
value to that passed object, which may update the value back in the tag library code for theresolveStrategy
to work, assuming it is the same object passed by reference.As a final option you could try assigning the
delegate/resolveStrategy
inside gsp curlies (${}
) in the template to see if that assigns the template object to thedelegate
parameter.