Section
The section is the most versatile and most used function in the template engine.
It allows for looping over arrays and numeric ranges, and conditional control of blocks and sequences.
It is controlled by a series of input parameter and sub functions.
Explanation of input parameters
name
Defines the namespace for all generated template variables, see loop and sequence for
a list of generated variables.
loop
Defines the data that the section should loop over, each the time the section loops it sets a
template variable and appends the result of all its children to the output. The data can either
be an array, in which case each item in the array is traversed, or a number that determines the
number of iterations (a negative number makes the iteration go backwards).
It's possible to constrain the number of elements that is iterated as well as single elements,
see parameters max and offset and sub-children section-exclude and section-include.
Each time the section iterates it sets four template variables in the new namespace. The variables
are index, number, key and item.
- index - is a number which starts at 0 and increases for each iteration.
- number - same as index but starts at 1
- key - if an array is iterated the key of the current item is set, if a number it will be the same as item
- item - if an array is iterated the current item is set, if a number the current iteration index is set (same as index)
show
This parameter determines whether the section block should be shown or not. If the parameter
is not present or is true (either a boolean true, non-empty array or non-zero value) the section block
is shown, otherwise the section-else block is shown. This is quite useful for conditional inclusion
of template code depending on a variable. When the section-else block is used no looping is done.
sequence
Defines a sequence which is iterated as the normal loop parameter, the difference is that the
sequence will wrap and only supports arrays. The current item will be set in the sequence template
variable. This parameter is useful if you want to create alternating colors in lists, for instance.
max
Determines the maximum number of iterations, the value must be an integer or an array, if it's an array
the count of the array is used.
offset
Determines the start of the loop array for the iterations, the value must be an integer or an array,
if it's an array the count of the array is used.
Explanation of sub children functions
section-else
Determines the start of the alternative block which is shown when show is false. See above.
delimiter
Determines a block of template elements which should be placed in between two iterations.
section-exclude and section-include
Adds a new filter rule for excluding or including a loop item, the rules will be run
after one-another as they are found. The rule will read the match parameter and
change the current accept/reject state for the current item, the default is to accept
all items. The match parameter can match any template variable available including loop iterators,
keys and items, but not the loop sequence.
Sequences and iteration counts will not be advanced if the loop item is discarded.
Examples
<h2>Showing the template variables for different loop types</h2>
<p>For each iteration you can see the template variables which are set by the section function,
they are <b>index</b>:<b>number</b>:<b>key</b></p>
<h3>Looping an array of numbers</h3>
{section name=Num loop=$numbers offset=2 max=2}
{$Num:index}:{$Num:number}:{$Num:key} Number: {$Num:item}<br/>
{/section}
<h3>Looping an associative array</h3>
{section name=Num loop=$assoc}
{$Num:index}:{$Num:number}:{$Num:key} Text: {$Num:item}<br/>
{/section}
<h3>Iterating 5 times</h3>
{section name=Num loop=5 sequence=array(red,blue)}
{section-exclude match=$Num:item|gt(3)}
{section-exclude match=$Num:item|lt(3)}
{section-include match=$Num:item|lt(2)}
{$Num:sequence}-{$Num:index}:{$Num:number}:{$Num:key} Number: {$Num:item}<br/>
{/section}
<h3>Iterating 5 times, backwards</h3>
{section name=Num loop=-5}
{$Num:index}:{$Num:number}:{$Num:key} Number: {$Num:item}<br/>
{/section}
<br/>
<h3>Looping over a multi-dim array</h3>
{* Looping over a multi-dim array and with a sequence *}
<table>
<th>URI</th><th>Name</th>
{section name=Loop loop=$menu:items sequence=array(odd,even)}
<tr><td>{$Loop:sequence} - {$Loop:item.uri}</td><td class={$Loop:sequence}>{$Loop:item.name}</td></tr>
{/section}
</table>
{* This section is controlled by the show parameter, if true the section is used (in this case false) *}
<p>Show list={$show_list|choose('off','on')}</p>
<p>{section name=Loop loop=$menu:items show=$show_list}
{$Loop:item.uri} : {$Loop:item.name}<br />
{/section}</p>
{* This section will only show the {section-else} part since the show item is false *}
{section name=Loop show=0}
<p>abc {$Loop:item} def</p>
{section-else}
<p>Shown for zero or empty vars</p>
{/section}
{* Numeric looping, also shows the use of the {delimiter} function *}
<h2>Loop 5 times</h2>
{section name=Loop loop=5}
{$Loop:item}
{delimiter}.{/delimiter}
{/section}
<h2>Loop 5 times negative</h2>
{section name=Loop loop=-5}
{$Loop:key}
{delimiter}::{/delimiter}
{/section}
Results
Showing the template variables for different loop types
For each iteration you can see the template variables which are set by the section function,
they are index:number:key
Looping an array of numbers
0:1:2 Number: 1003
Looping an associative array
0:1:red Text: Red
1:2:green Text: Green
2:3:blue Text: Blue
Iterating 5 times
red-0:1:0 Number: 1
blue-1:2:2 Number: 3
Iterating 5 times, backwards
0:1:0 Number: -1
1:2:-1 Number: -2
2:3:-2 Number: -3
3:4:-3 Number: -4
4:5:-4 Number: -5
Looping over a multi-dim array
| URI | Name | | odd - https://exponential.earth | eZ home | | even - http://zez.org | ZeZ | | odd - http://.ez.no/developer | eZ developer |
Show list=off
Shown for zero or empty vars
Loop 5 times1.2.3.4.5
Loop 5 times negative0::-1::-2::-3::-4
|