velocity: do something except in last loop iteration
16,728
Solution 1
You can use a test if you are in last iteration::
#foreach( $item in $list )
$item.text #if( $foreach.hasNext ), #end
#end
Solution 2
@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:
#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
.
Solution 3
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
Solution 4
This worked for me in an older version of Velocity
#if($velocityCount < $list.size()), #end
Related videos on Youtube
Author by
flybywire
Updated on January 30, 2020Comments
-
flybywire over 4 years
In velocity, I want to do something different in the last loop.
What is the correct idiom?
-
flybywire over 12 years@ratchet: same as related question: I am concatenating things with a separator in between and don't want to put the separator after the last item
-
-
holmis83 almost 8 yearsAt the time of writing, there is no version 2.0 of Velocity.
-
hyprstack almost 4 yearsHow would I solve this stackoverflow.com/questions/62713626/… ?