Grails 标签未解析其主体中的变量

发布于 2024-12-08 06:21:15 字数 1760 浏览 0 评论 0原文

我认为在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱世争霸 2024-12-15 06:21:15

传入参数的 body 是一个 Closure,它将其方法和参数解析到声明它的位置,这里将是主 gsp。您可以尝试将bodydelegate设置为标签库,并将resolveStrategy设置为Closure.DELEGATE_FIRST。这应该允许您解析 contentNamespace

def contentArea = {attrs, body ->
    ...
    def contentNamespace = "${attrs.contentAreaKey}" + "_contentList"
    out << g.render(
    ...
    body.delegate = this
    body.resolveStrategy = Closure.DELEGATE_FIRST
    out << body()
}

解析 minicontent_contentlist 会更困难,因为我不确定如何将模板指定为委托。您可以尝试在标签库中定义变量并将其传递到模板模型,然后将 minicontent_contentlist 值分配给该传递的对象,这可能会将该值更新回标签库代码中resolveStrategy 可以工作,假设它是通过引用传递的同一个对象。

def contentArea = {attrs, body ->
    ...
    def minicontent_contentList
    out << g.render( ..., model:[minicontent_contentList:minicontent_contentList])
    ...delegate and resolveStrategy stuff...
}

<b>In tag layout, </b>
<g:set var="minicontent_contentlist" value="bobby"/>
contentNamespace = ${contentNamespace}<br/><!-- prints "minicontent_contentList" -->
minicontent_contentList = ${minicontent_contentList}<br/> <!-- prints "bobby" -->

作为最后一个选项,您可以尝试在模板中的 gsp curlies (${}) 内分配 delegate/resolveStrategy ,看看是否将模板对象分配给 委托参数。

The body, as passed in parameter, is a Closure, 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 the delegate of the body to the tag library and set the resolveStrategy to Closure.DELEGATE_FIRST. This should allow you to resolve contentNamespace.

def contentArea = {attrs, body ->
    ...
    def contentNamespace = "${attrs.contentAreaKey}" + "_contentList"
    out << g.render(
    ...
    body.delegate = this
    body.resolveStrategy = Closure.DELEGATE_FIRST
    out << body()
}

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 the minicontent_contentlist value to that passed object, which may update the value back in the tag library code for the resolveStrategy to work, assuming it is the same object passed by reference.

def contentArea = {attrs, body ->
    ...
    def minicontent_contentList
    out << g.render( ..., model:[minicontent_contentList:minicontent_contentList])
    ...delegate and resolveStrategy stuff...
}

<b>In tag layout, </b>
<g:set var="minicontent_contentlist" value="bobby"/>
contentNamespace = ${contentNamespace}<br/><!-- prints "minicontent_contentList" -->
minicontent_contentList = ${minicontent_contentList}<br/> <!-- prints "bobby" -->

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 the delegate parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文