Scoping In Nested Dynamic Tag Bodies

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Scoping In Nested Dynamic Tag Bodies

Randall R Schulz
Hi,

I would like to be able write something like this in a GSP page (use
a fixed-width font):

 1  <table class='xref'>
 2    <tau:names xrColl="${xrColl}" names='all'>
 3      <tr><th class='xrName'>${it}</th></tr>  <!-- Call this ${it} is ${it0} -->
 4      <tau:xref xrColl="${xrColl}"
 5                list='sources'
 6                name="${it}">                 <!-- This ${it} is also ${it0} -->
 7        <tau:xref xrColl="${xrColl}"
 8                  list='refs'
 9                  name="${it0}"               <!-- Reference to ${it0} bound on line 2 -->
10                  source="${it}">             <!-- Reference to ${it} bound on lines 4-6 -->
11        ${it}                                 <!-- Reference to ${it} bound on lines 7-10 -->
12        </tau:xref>
13      </tau:xref>
14    </tau:names>
15  </table>


Is there a way to accomplish this? As it stands, each dynamic tag with a
body seems to shadow the ${it} that is in scope where it appears.


Randall Schulz

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Scoping In Nested Dynamic Tag Bodies

peter_m
Yeah, just define a new variable in your names loop:

 1  <table class='xref'>
 2    <tau:names xrColl="${xrColl}" names='all'>
         <g:def var="it0" value="${it}" />
 3      <tr><th class='xrName'>${it}</th></tr>  <!-- Call this ${it}
is ${it0} -->
 4      <tau:xref xrColl="${xrColl}"
 5                list='sources'
 6                name="${it}">                 <!-- This ${it} is
also ${it0} -->
 7        <tau:xref xrColl="${xrColl}"
 8                  list='refs'
 9                  name="${it0}"               <!-- Reference to
${it0} bound on line 2 -->
10                  source="${it}">             <!-- Reference to
${it} bound on lines 4-6 -->
11        ${it}                                 <!-- Reference to
${it} bound on lines 7-10 -->
12        </tau:xref>
13      </tau:xref>
14    </tau:names>
15  </table>
On 10/4/07, Randall R Schulz <[hidden email]> wrote:

> Hi,
>
> I would like to be able write something like this in a GSP page (use
> a fixed-width font):
>
>  1  <table class='xref'>
>  2    <tau:names xrColl="${xrColl}" names='all'>
>  3      <tr><th class='xrName'>${it}</th></tr>  <!-- Call this ${it} is ${it0} -->
>  4      <tau:xref xrColl="${xrColl}"
>  5                list='sources'
>  6                name="${it}">                 <!-- This ${it} is also ${it0} -->
>  7        <tau:xref xrColl="${xrColl}"
>  8                  list='refs'
>  9                  name="${it0}"               <!-- Reference to ${it0} bound on line 2 -->
> 10                  source="${it}">             <!-- Reference to ${it} bound on lines 4-6 -->
> 11        ${it}                                 <!-- Reference to ${it} bound on lines 7-10 -->
> 12        </tau:xref>
> 13      </tau:xref>
> 14    </tau:names>
> 15  </table>
>
>
> Is there a way to accomplish this? As it stands, each dynamic tag with a
> body seems to shadow the ${it} that is in scope where it appears.
>
>
> Randall Schulz
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Scoping In Nested Dynamic Tag Bodies

Randall R Schulz
On Thursday 04 October 2007 16:09, Peter Mondlock wrote:
> Yeah, just define a new variable in your names loop:

Ah. I see.

Thanks.


Randall Schulz


>  1  <table class='xref'>
>  2    <tau:names xrColl="${xrColl}" names='all'>
>          <g:def var="it0" value="${it}" />
>  3      <tr><th class='xrName'>${it}</th></tr>  <!-- Call this ${it} ${it0} -->
>  4      <tau:xref xrColl="${xrColl}"
>  5                list='sources'
>  6                name="${it}">                 <!-- This ${it} is also ${it0} -->
>  7        <tau:xref xrColl="${xrColl}"
>  8                  list='refs'
>  9                  name="${it0}"               <!-- Reference to ${it0} bound on line 2 -->
> 10                  source="${it}">             <!-- Reference to ${it} bound on lines 4-6 -->
> 11        ${it}                                 <!-- Reference to ${it} bound on lines 7-10 -->
> 12        </tau:xref>
> 13      </tau:xref>
> 14    </tau:names>
> 15  </table>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Scoping In Nested Dynamic Tag Bodies

Stand Trooper
Just as an FYI, this is the same approach I sometimes use with nested closures unless I define "it" for the closure

e.g.
greg = "bob was here"
def bob = {allen ->
    greg.each {
         if (it == allen) {
           println "woah there buddy")
         }
     }
}

etc.  This is a simplistic and unrealistic example, but you get the idea.  If I didn't define allen as "it" in the first closure, I would have to
"def allen = it" in order to do my comparison later on.

timo

On 10/4/07, Randall R Schulz <[hidden email]> wrote:
On Thursday 04 October 2007 16:09, Peter Mondlock wrote:
> Yeah, just define a new variable in your names loop:

Ah. I see.

Thanks.


Randall Schulz


>  1  <table class='xref'>
>  2    <tau:names xrColl="${xrColl}" names='all'>
>          <g:def var="it0" value="${it}" />
>  3      <tr><th class='xrName'>${it}</th></tr>  <!-- Call this ${it} ${it0} -->
>  4      <tau:xref xrColl="${xrColl}"
>  5                list='sources'
>  6                name="${it}">                 <!-- This ${it} is also ${it0} -->
>  7        <tau:xref xrColl="${xrColl}"
>  8                  list='refs'
>  9                  name="${it0}"               <!-- Reference to ${it0} bound on line 2 -->
> 10                  source="${it}">             <!-- Reference to ${it} bound on lines 4-6 -->
> 11        ${it}                                 <!-- Reference to ${it} bound on lines 7-10 -->
> 12        </tau:xref>
> 13      </tau:xref>
> 14    </tau:names>
> 15  </table>
Reply | Threaded
Open this post in threaded view
|

Re: Scoping In Nested Dynamic Tag Bodies

graemer
In reply to this post by Randall R Schulz
Also say your outer tag is this

def outer = { attrs, body ->
    out <<  body(foo:"bar")
}
def inner = { attrs, body ->
    out << body()
}

Then in the tags you can referenced the variable foo:

<g:outer>
     <g:inner>${foo}</g:inner>
</g:outer>

This works in curent SVN

Cheers

On 10/5/07, Randall R Schulz <[hidden email]> wrote:

> On Thursday 04 October 2007 16:09, Peter Mondlock wrote:
> > Yeah, just define a new variable in your names loop:
>
> Ah. I see.
>
> Thanks.
>
>
> Randall Schulz
>
>
> >  1  <table class='xref'>
> >  2    <tau:names xrColl="${xrColl}" names='all'>
> >          <g:def var="it0" value="${it}" />
> >  3      <tr><th class='xrName'>${it}</th></tr>  <!-- Call this ${it} ${it0} -->
> >  4      <tau:xref xrColl="${xrColl}"
> >  5                list='sources'
> >  6                name="${it}">                 <!-- This ${it} is also ${it0} -->
> >  7        <tau:xref xrColl="${xrColl}"
> >  8                  list='refs'
> >  9                  name="${it0}"               <!-- Reference to ${it0} bound on line 2 -->
> > 10                  source="${it}">             <!-- Reference to ${it} bound on lines 4-6 -->
> > 11        ${it}                                 <!-- Reference to ${it} bound on lines 7-10 -->
> > 12        </tau:xref>
> > 13      </tau:xref>
> > 14    </tau:names>
> > 15  </table>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Scoping In Nested Dynamic Tag Bodies

Randall R Schulz
On Friday 05 October 2007 01:04, Graeme Rocher wrote:

> Also say your outer tag is this
>
> def outer = { attrs, body ->
>     out <<  body(foo:"bar")
> }
> def inner = { attrs, body ->
>     out << body()
> }
>
> Then in the tags you can referenced the variable foo:
>
> <g:outer>
>      <g:inner>${foo}</g:inner>
> </g:outer>

OK. That's good, too.

To go back to my (unanswered) question about "Dynamic Tags With
Multiple-Argument Body Closures?":

On Thursday 04 October 2007 10:07, Randall R Schulz wrote:
> Hi,
>
> Is it possible to have dynamic tags with bodies whose body closure
> accepts multiple arguments? If so, how does the tag body refer to
> these arguments? All the examples I've seen show only one parameter
> and refer to the parameter by the default name "it".

I take it the only way to handle this is use a map to pass multiple
arguments and not literally give the tag body closure more than the two
arguments, "attrs" and "body"?


> This works in curent SVN

Is it a very recent change? I'm currently using a 1.0-RC1-SNAPSHOT
release from Oct. 1.


> Cheers


Randall Schulz

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email