[ 
https://issues.apache.org/jira/browse/VELOCITY-598?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12622674#action_12622674
 ] 

Jarkko Viinamäki commented on VELOCITY-598:
-------------------------------------------

I'm not sure if this can be classified as a bug. I took a quick peek at the 
Foreach directive code and it seems that the counter ($num) value is saved only 
in the local context. Thus the value is not available for macro invocations if 
the variable is not passed as an argument. This seems logical. There are two 
ways to fix this: 

1) pass $num as an argument to the numMacro call

#macro(numMacro $dum)$dum (numMacro)#end

#macro(outerMacro)
    #foreach($num in [1, 2, 3])
        from loop body: $num
        From Macro: #numMacro($num)
    #end
#end
#outerMacro() 


2) use a helper variable.

#macro(numMacro)$dum (numMacro)#end

#macro(outerMacro)
    #foreach($num in [1, 2, 3])
        #set($dum = $num)
        from loop body: $num
        From Macro: #numMacro()
    #end
#end
#outerMacro() 

I wouldn't advice redefining the macro inside a foreach loop.

> Macros within macros are losing the context
> -------------------------------------------
>
>                 Key: VELOCITY-598
>                 URL: https://issues.apache.org/jira/browse/VELOCITY-598
>             Project: Velocity
>          Issue Type: Bug
>          Components: Engine
>    Affects Versions: 1.5
>         Environment: velocimacro.permissions.allow.inline.local.scope=true
> velocimacro.permissions.allow.inline=true
> velocimacro.context.localscope=false
> (we can not change this!)
>            Reporter: Jörg Gottschling
>            Priority: Critical
>
> consider the following test code:
> #foreach($num in [1, 2, 3])
>     from loop body: $num
>     #macro(numMacro)$num (numMacro)#end
>     From Macro: #numMacro()
> #end
> it produces correctly:
> from loop body: 1
> From Macro: 1 (numMacro)
> from loop body: 2
> From Macro: 2 (numMacro)
> from loop body: 3
> From Macro: 3 (numMacro)
> But if you wrap this in a macro again, it fails:
> #macro(outerMacro)
>     #foreach($num in [1, 2, 3])
>         from loop body: $num
>         #macro(numMacro)$num (numMacro)#end
>         From Macro: #numMacro()
>     #end
> #end
> #outerMacro()
> output:
> from loop body: 1
> From Macro: $num (numMacro)
> from loop body: 2
> From Macro: $num (numMacro)
> from loop body: 3
> From Macro: $num (numMacro)
> That seams to be a bug for me, when considereing the settings above.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


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

Reply via email to