1

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...

Krishnaveni
  • 799
  • 2
  • 11
  • 32

3 Answers3

2

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
Chris Kent
  • 21
  • 4
1

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.

Nathan Bubna
  • 6,823
  • 2
  • 35
  • 36
0

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

Bart
  • 19,692
  • 7
  • 68
  • 77
seba.wagner
  • 3,800
  • 4
  • 28
  • 52