FuzzyLogic
FuzzyLogic

Reputation: 61

Velocity - Passing current forloop variable to another template

I have a Velocity template where I am using a for loop and within it, executing a parse statement. The problem is the parse statement needs access to the current object in the for loop but it seems to go out of scope. Having searched on here, I tried one suggestion to create a variable and assign the current variable in the iteration to it, but it only works for the first iteration. All subsequent iterations contain a reference to the first object in the iteration. An example:

  #foreach ($someObject in $MyList)
       #set($anotherObject=$someObject)
       #parse('innerTemplate.vm')
  #end

The problem is innerTemplate.vm never sees $someObject, so if I assign it to another variable using the set construct, it only remembers the first item in the list.

Upvotes: 6

Views: 7408

Answers (1)

Francisco Paulo
Francisco Paulo

Reputation: 6322

Velocity already provides a way to get the loop count through $velocityCount.

Try this:

outerTemplate.vm:

#foreach ($someObject in $MyList)
   #parse('innerTemplate.vm')
#end

innerTemplate:

$velocityCount

Upvotes: 15

Related Questions