velocity: do something except in last loop iteration

ε祈祈猫儿з 提交于 2019-11-30 16:26:21

问题


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


回答1:


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

#foreach( $item in $list )
    $item.text #if( $foreach.hasNext ), #end
#end



回答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.




回答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



回答4:


This worked for me in an older version of Velocity

#if($velocityCount < $list.size()), #end


来源:https://stackoverflow.com/questions/8196828/velocity-do-something-except-in-last-loop-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!