Loop Object

The Loop object allows you to perform actions on a set of data, until the specified conditions are met.

Size

Returns the size of the data set or undef if unknown.

Copy
    <p>You have [* products.size *] products to look at!</p>

Max

Returns the maximum index number (i.e. the index of the last element) which is equivalent to size() -1.

Index

Returns the current index number which is in the range 0 to max()

Copy
    [* products = [
        {
            name = 'Product One'
        },
        {
            name = 'Product Two'
        },
        {
            name = 'Product Three'
        },
        {
            name = 'Product Four'
        },
        {
            name = 'Product Five'
        },
        {
            name = 'Product Six'
        },
    ] *]
    
    [* FOREACH product IN products *]
        <p>Loop number [* loop.index *]</p>    [* END *]

Count

Returns the current iteration count in the range 1 to size(). This is equivalent to index() +1.

Copy
[* FOREACH product IN products *]
    <p>Product number [* loop.count *]</p>
[* END *]

First

Returns a boolean value to indicate if the iterator is currently on the first iteration of the set.

The following example will only display the first product.

Copy
    [* FOREACH product IN products *]
        [* IF loop.first *]
            <p>This is the first product!</p>
        [* END *]
    [* END *]

Last

Returns a boolean to indicate if the iterator is currently on the last iteration of the set.

The following example will only display the last product.

Copy
    [* FOREACH product IN products *]
        [* IF loop.last *]
            <p>This is the last product!</p>
        [* END *]
    [* END *]

Prev

Returns the previous item in the data set, or undef if the iterator is on the first item.

The following example will display various combinations of foods.

Copy
[* food_list = [
    {
        name = 'Eggs'
    },
    {
        name = 'Beans'
    },
    {
        name = 'Bacon'
    },
    {
        name = 'Cheese'
    },
    {
        name = 'Bread'
    },
    {
        name = 'Soup'
    },
] *]

<p>The following foods go well together:</p>
[* FOREACH food IN food_list *]
    [* # check we're not looking at the first food item *]
    [* IF !loop.first *]
        [* previous_food = loop.prev *]
        <p>[* previous_food.name *] AND [* food.name *]</p>
    [* END *]
[* END *]

Next

Returns the next item in the data set or undef if the iterator is on the last item.

The following example will always skip the first product.

Copy
    [* FOREACH product IN products *]
        [* # check we're not looking at the last product *]
        [* IF !loop.last *]
            [* next_product = loop.next *]
            <p>Have a look at [* next_product.name *]</p>
        [* END *]
    [* END *]

Parity

Returns the text string even or odd to indicate the parity of the current iteration count (starting at 1). This is typically used to create striped zebra tables.

Copy
    <style>
    tr.odd td {
        background-color: black;
        color: white;
    }

    tr.even td {
        background-color: white;
        color: black;
    }
    </style>

    <table>
    [* FOREACH product IN products *]
        <tr class="[* loop.parity *]">
            <td>[* product.name *]</td>
        </tr>
    [* END *]
    </table>

Odd

Returns a boolean (0/1) value to indicate if the current iterator count (starting at 1) is an odd number.

This means it will return a true value for the first iterator, the third, fifth etc. This can be used to display a list of products in two table columns.

Copy
<table>
<tr>
    [* FOREACH product IN products *]
        [* IF loop.odd *]
            </tr><tr>
        [* END *]
        <td>[* product.name *]</td>
    [* END *]
</tr>
<table>

Note: You can also use loop.index with this technique to display data in multiple columns.

Copy
<table>
<tr>
    [* FOREACH product IN products *]
        [* IF loop.index % 3 == 0 *]
            </tr><tr>
        [* END *]
        <td>[* product.name *]</td>
    [* END *]
</tr>
<table>

Even

Returns a boolean (0/1) value to indicate if the current iterator count (starting at 1) is an even number.

This returns a true value for the second iteration, fourth, sixth etc.