Actually I don't think it's all that odd, the error case is somewhat general (it came up in my own usage of Velocity to generate HTML lists). Any pass by name reference to the #foreach iteration variable will fail, it doesn't need to be in a string literal, for example:

#macro(test $x)#foreach($i in [1, 2])$x#end#end
#test($i)

Maybe a different disscussion, but somewhat related to how #foreach is implemented with local scope. The macro localscope setting (velocimacro.context.localscope) has the same behavior as above for the #set directive. However, I'm not sure what Velocity's intended behavior is with scoping rules and pass by name.

Nathan Bubna wrote:
to my knowledge, macro arguments have always been passed by name,
even string literal ones.  still, this is an odd case, using the
foreach index reference in a string literal passed to a macro that
includes the foreach which defines that reference.  yeah, that's a
bug, but my, what a tangled web it took to catch it. :)

On Sun, Oct 12, 2008 at 7:08 PM, Will Glass-Husain <[EMAIL PROTECTED]> wrote:
I would have assumed the quoted string would be evaluated before
being passed in to the macro.  Hence $i is invalid reference when
it is evaluated, and we'd always see "no no no".

WILL

On Sun, Oct 12, 2008 at 5:17 PM, Byron Foster <[EMAIL PROTECTED]>
wrote:

I went back and pulled the velocity source to the revision
(471881) before the changes to bug 285 and found that the
behavior is as expected, so I conclude this is a bug :) which has
been created here:

https://issues.apache.org/jira/browse/VELOCITY-630


On Oct 10, 2008, at 17:04 , Byron Foster wrote:

Given the following:
#macro(test $a $b) #foreach($i in $a) $b #end #end #test( [1,
2, 3] "#if($i == 2) yes #else no #end")

I would expect the output:

no  yes  no

Instead, I get

no  no  no

However, if I have

#macro(test $a $b) #set($i = 1) $b #set($i = 2) $b #set($i = 3)
$b #end #test( [1, 2, 3] "#if($i == 2) yes #else no #end")

then I get: no  yes  no as expected.

I would say there is a bug here.  The Foreach directive
explicitly sets $i in the localscope.  If I replace the
explicit put to the localscope with a regular put, the first
macro case works.  I was looking at the following issues where
the modification to Foreach was mode.

https://issues.apache.org/jira/browse/VELOCITY-285

Now, my test change to Foreach breaks issue 285, but I'm not
convinced that the resolution to 285 is complete.  Thoughts?

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to