#foreach

Loops through a list of objects

Examples :

Loop through an array :

    #foreach ( $item in [1..8] ) ## from 1 to 8
    . item = $item
    #end
   
    #foreach ( $item in [3,2,1] ) ## from 3 to 1
    . item = $item
    #end
   
    ## Everything in a single line
    #foreach ( $item in ['this', 'is', 'a', 'loop' ] ) ${item}#end


Loop through an object (array, list, collection) :

    #foreach( $attribute in $entity.attributes )
    $attribute.type $attribute.name
    #end


Loop counter provided by "$foreach.count" ( 1 to N ) :

    #foreach ( $item in ["A", "B", "C", "D" ] )
    $foreach.count : $item
    #end


Map iteration :

    #set ( $map = {"banana" : "good", "cream" : "bad"} )
    #foreach($key in $map.keySet() )
    $key --> $map.get($key)
    #end


Break the current iteration :

    #foreach ( $item in [1..20] )
    #if ( $item > 3 ) #break #end
    . item = $item
    #end