2 <:for:>

<:for expr [name=sequence_item]> 
    [ <:break:> ]
    [ <:continue:> ]
    [ <:else:> ] 
<:/for:>

The <:for:><:/for:> tag implements a loop: it expects the expression in expr to return a sequence of items (a tuple or list, for example). For each item in the sequence, <:for:> assigns the item to the variable name given in the name argument, and does whatever is inside the <:for:> tag. The name parameter is optional; if not specified, <:for:> will assign each item to a local variable called sequence_item.

If the sequence in expr is empty or false, <:for:> will do nothing, unless you put an <:else:> tag inside the <:for:> block. The <:for:> tag will execute everything after an <:else:> tag if the sequence in expr is empty.

Examples:

<:for `(1,2,3)`:>
current loop value: <:val `sequence_item`:><BR>
<:/for:>

<:for `[1,2,3]` foo:> 
current loop value: <:val `foo`:><BR> 
<:/for:>

<:for `[]` foo:>
This wouldn't show
<:else:>
Loop is empty
<:/for:>

There are two special tags which allow you to force either an exit from the loop or an immediate return to the top of the loop: <:break:> and <:continue:>. The <:break:> tag will cause an immediate exit from the <:for:> tag. The <:continue:> tag will cause an immediate return to the top of the <:for:> tag, where it will assign the next item in the sequence to the variable, or if there are no items left in the sequence, it will exit the <:for:> tag. For example, this STML block will print the numbers 1 2 3:

<:set myList `[1,2,3,4,3,2,1]`:>
<:for `myList` item:>
   <:if `item > 3`:>
       <:break:>
   <:/if:>
   <:val `item`:>
<:/for:>

And this STML block will print the numbers 1 2 3 3 2 1:

<:set myList `[1,2,3,4,3,2,1]`:>
<:for `myList` item:>
   <:if `item > 3`:>
       <:continue:>
   <:/if:>
   <:val `item`:>
<:/for:>