速度 - 将当前 forloop 变量传递给另一个模板 [英] Velocity - Passing current forloop variable to another template

查看:18
本文介绍了速度 - 将当前 forloop 变量传递给另一个模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Velocity 模板,我在其中使用了 for 循环并在其中执行了 parse 语句.问题是 parse 语句需要访问 for 循环中的当前对象,但它似乎超出了范围.在这里搜索后,我尝试了一个建议来创建一个变量并将迭代中的当前变量分配给它,但它仅适用于第一次迭代.所有后续迭代都包含对迭代中第一个对象的引用.一个例子:

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

问题是innerTemplate.vm 从来没有看到$someObject,所以如果我使用set 构造将它分配给另一个变量,它只会记住列表中的第一项.

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.

推荐答案

Velocity 已经提供了一种通过 $velocityCount 获取循环计数的方法.

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

试试这个:

outerTemplate.vm:

outerTemplate.vm:

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

内部模板:

$velocityCount

这篇关于速度 - 将当前 forloop 变量传递给另一个模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆