Velocity - Passing current forloop variable to another template

旧街凉风 提交于 2019-12-08 19:32:23

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/7791075/velocity-passing-current-forloop-variable-to-another-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!