Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

velocity: do something except in last loop iteration

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

like image 903
flybywire Avatar asked Nov 19 '11 19:11

flybywire


2 Answers

You can use a test if you are in last iteration::

#foreach( $item in $list )     $item.text #if( $foreach.hasNext ), #end #end 
like image 128
soulcheck Avatar answered Oct 05 '22 10:10

soulcheck


@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.

like image 23
Mark Rhodes Avatar answered Oct 05 '22 10:10

Mark Rhodes