In velocity, I want to do something different in the last loop.
在速度方面,我想在最后一个循环中做一些不同的事情。
What is the correct idiom?
什么是正确的习语?
RELATED: Last iteration of enhanced for loop in java
相关:java中增强for循环的最后一次迭代
4 个解决方案
#1
43
You can use a test if you are in last iteration::
如果您在最后一次迭代中,可以使用测试::
#foreach( $item in $list )
$item.text #if( $foreach.hasNext ), #end
#end
#2
15
@soulcheck's answer is what you need, but be aware that the $foreach
variable is only available in velocity 1.7, if you're using an earlier version you can use:
@schechecheck的答案是你需要的,但请注意$ foreach变量仅在速度1.7中可用,如果你使用的是早期版本,你可以使用:
#foreach( $item in $list )
$item.text #if( $velocityHasNext ), #end
#end
However, the $velocityHasNext
variable is deprecated in versions 1.7 and removed in 2.0 in favour of $foreach.hasNext
.
但是,$ velocityHasNext变量在版本1.7中已弃用,在2.0中已删除,而使用$ foreach.hasNext。
#3
5
The idiom I use is to save the optional text to be added if the loop doesn't finish.
我使用的习惯用法是保存在循环未完成时要添加的可选文本。
#set($sep = "")
#foreach($item in $list)
$sep$item
#set($sep = ", ")
#end
#4
1
This worked for me in an older version of Velocity
这在Velocity的旧版本中对我有用
#if($velocityCount < $list.size()), #end
#1
43
You can use a test if you are in last iteration::
如果您在最后一次迭代中,可以使用测试::
#foreach( $item in $list )
$item.text #if( $foreach.hasNext ), #end
#end
#2
15
@soulcheck's answer is what you need, but be aware that the $foreach
variable is only available in velocity 1.7, if you're using an earlier version you can use:
@schechecheck的答案是你需要的,但请注意$ foreach变量仅在速度1.7中可用,如果你使用的是早期版本,你可以使用:
#foreach( $item in $list )
$item.text #if( $velocityHasNext ), #end
#end
However, the $velocityHasNext
variable is deprecated in versions 1.7 and removed in 2.0 in favour of $foreach.hasNext
.
但是,$ velocityHasNext变量在版本1.7中已弃用,在2.0中已删除,而使用$ foreach.hasNext。
#3
5
The idiom I use is to save the optional text to be added if the loop doesn't finish.
我使用的习惯用法是保存在循环未完成时要添加的可选文本。
#set($sep = "")
#foreach($item in $list)
$sep$item
#set($sep = ", ")
#end
#4
1
This worked for me in an older version of Velocity
这在Velocity的旧版本中对我有用
#if($velocityCount < $list.size()), #end