You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by hacking bear <ha...@hotmail.com> on 2003/01/02 21:59:22 UTC

Re: Variable scoping in recurse parse (or macro)

Thanks. I checked out the contrib in download area, there is only a struts 
related stuff there. Where should I look for this #local plugin.

I attempt to turn my template into a giant macro and do recursive calls of 
the macro but still get the same scoping issue. So I do a simply macro 
recursive call test, shown below;

<!--
#macro (recurse $val $count)
#if ( $count > 0)
BEFORE recurse $count $val
#set ( $c = $count - 1 )
#recurse("${val}.${val}" $c )
AFTER recurse $count $val
#end## if count > 0
#end## recurse

#set ( $count = 3 )
start count $count
#recurse("hello" $count)
end count $count
-->

and the output:
<!--

start count 3
BEFORE recurse 3 hello
BEFORE recurse 2 hello.hello
BEFORE recurse 1 hello.hello.hello.hello
AFTER recurse 0 hello.hello.hello.hello
AFTER recurse 0 hello.hello
AFTER recurse 3 hello
end count 3
-->

>From the output, the value of 'val' is kept locally in the macro but the 
value of count is not. I also did the following test:

TEST 2
#set ( $count = 3 )
#set ( $val = "hello" )
start count $count $val
#recurse($val $count)
end count $count $val

The result is the same. Any idea why is it the case?

>From: "Nathan Bubna" <na...@esha.com>
>Reply-To: "Velocity Users List" <ve...@jakarta.apache.org>
>To: "Velocity Users List" <ve...@jakarta.apache.org>
>Subject: Re: Variable scoping in recurse parse
>Date: Wed, 1 Jan 2003 21:12:07 -0800
>
>hacking bear said:
> > I have been assuming that velocity #parse() would create its own local
> > context from the caller scope. But it seems variable changes in local
>scope
> > are visible in the caller. For example:
>...
>
>yes, this is the designed behavior
>
> > The problem is that once in the recursive call "foo" is set to false, 
>the
> > "foo" value in the parent call is also false.
> >
> > I have self-recursive templates which depend on the parent's variable 
>not
>to
> > be changed. What should I do?
>
>though i've never used it myself, rumor has it that there is a #local()
>directive that geir stuck in the contrib area.  my understanding is that, 
>if
>you plug that directive in, you should be able to put that around your
>#parse calls and achieve the behavior you desire.
>
>Nathan Bubna
>nathan@esha.com
>
>
>--
>To unsubscribe, e-mail:   
><ma...@jakarta.apache.org>
>For additional commands, e-mail: 
><ma...@jakarta.apache.org>


_________________________________________________________________
Help STOP SPAM: Try the new MSN 8 and get 2 months FREE* 
http://join.msn.com/?page=features/junkmail


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Variable scoping in recurse parse (or macro)

Posted by Nathan Bubna <na...@esha.com>.
hacking bear said:
> Thanks. I checked out the contrib in download area, there is only a struts
> related stuff there. Where should I look for this #local plugin.

no, the contrib isn't in the download area.  it's in the cvs repository.
but i took a second to look and actually, the class for the #local directive
is in geir's whiteboard area:

http://cvs.apache.org/viewcvs/jakarta-velocity/whiteboard/geir/

but again, i must disclaim: i haven't used this myself ever.  if you need
more help with it, someone else will have to help you.

> I attempt to turn my template into a giant macro and do recursive calls of
> the macro but still get the same scoping issue. So I do a simply macro
> recursive call test, shown below;
...
> The result is the same. Any idea why is it the case?

because that's the default behavior.  the question you should be asking is:

 "is there a sensible way to change this?"

and the answer is:

"yes, this is mentioned under the velocimacro section of the user's guide
and the velocity configuration section of the developer's guide"

i'd repeat it for you here, but then i couldn't be sure that you'd read that
section for yourself.  :-)

here's the links:

http://jakarta.apache.org/velocity/user-guide.html#Velocimacros
http://jakarta.apache.org/velocity/developer-guide.html#Velocity%20Configura
tion%20Keys%20and%20Values

oh, and just for good measure, here's a link to the mailing list archives:

http://nagoya.apache.org/eyebrowse/SummarizeList?listId=103

(hint:  this is my gentle way of suggesting that you follow proper edicate
and check the manuals and search the mailing list archives before posting
questions to the list.)

Nathan Bubna
nathan@esha.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>