Question

My code looks like this:

#macro (myMac $listOfValues)
    #foreach ($val in ${listOfValues})
        #set ($subList = $val.child())
        #if (some condition)
            some output
            #if (${velocityCount} < ${listOfValues.size()})
               ,\n
            #end
        #else
            #myMac(${subList})  -- B
        #end
    #end
#end
#myMac (${listOfValues})  -- A

listOfValues -- is a list of string subList -- is a list of String

${listOfValues.size() is always zero, during the recursive call (call from B), though the list has more than one value. However when the macro is called from A the size is correct. Can you please point out if there is something missing...

No correct solution

OTHER TIPS

I just came across a similar problem, I found a post that helped with recursive macros.

Basically it suggested de-referencing the macro parameters, and using local copies instead, I believe this will solve the problem...

So try

#macro (myMac $listOfValues)
#set ($localValues = $listOfValues) ##dereference parameters
#foreach ($val in ${localValues})
    #set ($subList = $val.child())
    #if (some condition)
        some output
        #if (${velocityCount} < ${localValues.size()})
            ,\n
        #end
    #else
        #myMac(${subList})  -- B
    #end
#end
#end
#myMac (${listOfValues})  -- A

Every call to #myMac includes a new #foreach, every #foreach sets its own $velocityCount

This is one of the reasons it is deprecated. You can't access the $velocityCount for the parent #foreach.

In v1.7, you can use $foreach.count for the current loop and $foreach.parent.count for the parent and $foreach.parent.parent.count and so on.

Or you could just make your own counter.

Maybe velocityCount is not designed for recursive usage. Velocity has its limitations. You might easily workaround by setting your own counter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top