I’m trying to create a velocity template with a ‘for each’ loop like so
#foreach ($Key in $!{KeyList})
#if $Key1Function == something, write something #end
#end
I would like the number for the KeyFunction to change as I iterate through the loop.
So I tried writing $Key$foreach.countFunction, $Key{$foreach.count}Function and $Key{$foreach.count}$Function, which unsurprisingly all failed.
I have also tried using #set (within the foreach loop), ie
#set($a = "KeyFunction")
#set($b = "foreach.count") ##(or velocityCount -- same problem)
#set($c = "$a$b")
but now when I inspect $c I get KeyFunction1, and when I try and $ this (ie $$c) I just get $KeyFunction1 — I can’t get Velocity to evaluate again.
I have tried using #evaluate in all manner of places and ways but it doesn’t appear to do anything.
EDIT – If it makes the problem much simpler I can rewrite $Key1Function as $KeyFunction1, which presumably needs fewer ‘$’ signs to work?
Can anyone help me with the correct syntax for this problem?
Thanks
Evaluate doesn’t return values. It just renders content into the output stream. The RenderTool in VelocityTools does return values. But for this, you don’t need that much power. Just put the context into itself:
and ask it for the value you want, using an interpolated string (double quotes) to create the key. So, something like this: