24

I'm looking for a certain condition by using foreach to iterate through a collection (of permissions). So if I find all that I need and don't need to loop anymore, is there a way to break out of the loop? I am new to velocity and trying to grok this weird language.

#foreach ($perm in $space.getPermissions())  
#end
barneytron
  • 7,943
  • 3
  • 23
  • 25
  • Velocity does not provide a #break, however there are ways to do the same thing:[http://mail-archives.apache.org/mod_mbox/velocity-user/200310.mbox/%3C3F8541F2.70303@dlr.de%3E](http://mail-archives.apache.org/mod_mbox/velocity-user/200310.mbox/%3C3F8541F2.70303@dlr.de%3E) – Nick Jurista Oct 27 '09 at 00:40

1 Answers1

62

The latest version of Velocity (1.6) contains a statement #break

https://velocity.apache.org/engine/1.6.2/user-guide.html#Loops

## list first 5 customers only
#foreach( $customer in $customerList )
    #if( $velocityCount > 5 )
        #break
    #end
    $customer.Name
#end
starwarswii
  • 2,187
  • 1
  • 16
  • 19
Will Glass
  • 4,800
  • 6
  • 34
  • 44