[
https://issues.apache.org/jira/browse/VELOCITY-598?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12622721#action_12622721
]
Nathan Bubna commented on VELOCITY-598:
---------------------------------------
Jorg, i can see how what happens in your example is unintuitive, and if someone
comes up with a clean, simple patch to "fix" to make you example work, then i
won't oppose it. But i'm also very hesitant to call this a bug because of the
way velocimacros are being abused here.
Specifically, i would consider both the example at the top and option #2 to be
bad practice, both because it repeatedly defines a macro in both a loop and in
another macro and because the macro being defined treats a local variable as if
it were global or template scoped. Both are bad ideas, if you ask me. Any
variables a macro needs should either be available everywhere in the current
template (template scoped) or else be passed into the macro as an argument.
Otherwise than can never be any guarantee that the needed reference is in scope
when the macro is called. Velocity is not javascript and can't promise access
to $num just because you defined #numMacro within the #foreach. Closures are
not supported here and macros are definitely NOT like functions.
If anything, this makes me wonder if we should/could block macro definition
from happening within #foreach and other #macro calls. Both are terrible ideas
that seem to be born out of a confusion between Velocity and javascript (or
someother scripting language). Given the choice between making things work as
you desire and throwing a syntax error when processing a template such as your
example, i would consider the latter to be wiser.
> 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]