Skip to content
Nat! edited this page Mar 1, 2023 · 4 revisions

A for statement iterates over an expression, which is typically an array,

This is an example of a for loop over an array.

Example 1

{% for item in @( "a", @"b", @"c") %}
<b>{{ item }}</b>
{% endfor %}

Output:

<b>a</b>
<b>b</b>
<b>c</b>

You can write out the data in a more formatted kind of way, thanks to some built-in special variables that are used during a for loop.

To the variable that is looping over the array contents an additional dictionary of the same name with a '#' appended is created. In the example above that would be item# then. This dictionary contains values that are calculated for each loop step. They are:

Name Description
header see below
footer see below
division see below
modulo see below
i the loop count
previ the loop count - 1
nexti the loop count + 1
isFirst will be set YES, if it's the first loop iteration
isEven will be set YES, if the loop count is even (0,2,4 ...)
isLast will be set YES, if it's the last loop iteration
evenOdd the contents of the MulleScionEven variable if isEven is true, otherwise the

There are a few predefined variables that are used in for loops to allow easier formatting of common output, like lists with parentheses and commas. Here item# will refer to the iterator dictionary of the for loop.

Name Description
MulleScionForOpener The value for item#.header for the first loop iteration
MulleScionForSeparator The value for item#.footer for all but the last loop iterations
MulleScionForCloser The value for item#.footer for the last loop iteration

Example 2

{% MulleScionForOpener    = "[" %}
{% MulleScionForSeparator = "/" %}
{% MulleScionForCloser    = "]" %}
{% for item in @( 1, 2, 3 ) %}{{ item#.header }}{{ item }}{{ item#.footer }}{% endfor %}

Output:

[1/2/3]

Example 3

{%
   MulleScionForOpener    = "OPEN"
   MulleScionForSeparator = "SEPA"
   MulleScionForCloser    = "CLOS"
%}

{% for item in @( @"a", @"b", @"c") %}
i={{ item#.i }}, header={{ item#.header }}, footer={{ item#.footer }}, division={{ item#.division }}, modulo={{ item#.modulo }}, isEven={{ item#.isEven }}, isFirst={{ item#.isFirst }}, isLast={{ item#.isLast }}, evenOdd={{ item#.evenOdd }}
{% endfor %}

Output:


i=0, header=OPEN, footer=SEPA, division=0, modulo=0, isEven=YES, isFirst=YES, isLast=NO, evenOdd=even
i=1, header=, footer=SEPA, division=1, modulo=0, isEven=NO, isFirst=NO, isLast=NO, evenOdd=odd
i=2, header=, footer=CLOS, division=2, modulo=0, isEven=YES, isFirst=NO, isLast=YES, evenOdd=even

There are a few other variables, that can be helpful dealing with row coloring for example:

Name Description
MulleScionEven The value to be used for item#.evenOdd on even loop iterations
MulleScionOdd The value to be used for item#.evenOdd on odd loop iterations
MulleScionForSubdivisionLength Divides the loop into subdivisions, setting the item#.modulo
MulleScionForSubdivisionOpener Will be the value of item.#header whenever the modulo is 0 (
MulleScionForSubdivisionCloser Will be the value of item.#footer whenever the modulo is MulleScionForSubdivisionLength - 1 (vulgo: the end of a subdivision :) but not on the very last line.

Example 4

{%
   MulleScionForSubdivisionLength = 2
   MulleScionForSubdivisionOpener = "-["
   MulleScionForSubdivisionCloser = "]"

   MulleScionForSeparator         = ","
   MulleScionForOpener            = "-[["
   MulleScionForCloser            = "]]"

   for item in @( 1, 2, 3, 4)
      {{ item#.header }}{{ item }}{{ item#.footer }}
   endfor
%}

Output:

-[[1,2]-[3,4]]

If the array over which to iterate in the for loop is empty, you can detect this by using elsefor. Here is a more complex example, that lists the available NSTimeZones, also using elsefor in case none are available.

Example 5

{% MulleScionEven = "#DDDDDD"
   MulleScionOdd  = "#FFFFFF"
 %}
{% for item in [NSTimeZone knownTimeZoneNames] %}
    {% if item#.isFirst %}
    <table>
       <tr><th>TimeZone</th></tr>
    {% endif %}
       <tr bgcolor="{{ item#.evenOdd }}"><td>{{ item }}</td></tr>
    {% if item#.isLast %}
    </table>
    {% endif %}
{% elsefor %}
Sorry, no timezone info available.
{% endfor %}

Output:

Sorry, no timezone info available.
Clone this wiki locally