【问题标题】:Velocity : Breaking out of foreach-loop dynamically速度:动态打破 foreach 循环
【发布时间】:2018-12-15 20:39:19
【问题描述】:

我需要在 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»

【问题讨论】:

  • 看起来你的情况不对$foreach.hasNext==false这里。
  • 在官方API中发现是这样的:(

标签: velocity


【解决方案1】:
#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 开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2014-03-24
    • 2018-07-01
    • 1970-01-01
    相关资源
    最近更新 更多