速度:动态突破foreach循环

时间:2018-12-14 11:14:11

标签: velocity

我需要在Velocity中实现一个解决方案,以打破数组 forelast 项中的foreach循环。有了API,我开始构建它,但是我没有中断,仍然显示了循环的所有行。 有没有人对我有更好的工作方式?

«#if($special)»
  «#foreach($item in $sum)»
    «#if($foreach.hasNext==false)» «#break» «#end»
    «do special stuff» 
  «#end»
«#else»
  «#foreach($item in $sum)»
    «do normal stuff»
  «#end»
«#end»

1 个答案:

答案 0 :(得分:0)

#if(!$foreach.hasNext) #break #end

#if($foreach.hasNext == false) #break #end

都应适用于Velocity 1.7+。如果对此有疑问,可以显示

$foreach.hasNext

在循环中,并检查它是否显示true true true ... false

对于以前版本的Velocity,您必须自己做:

#set($count = $num.size())
#foreach($item in $num)
  #if($velocityCount < $count)
    ...
  #end
#end

请注意,$velocityCount变量(在1.7中已弃用,在2.0中消失了)从1开始。