ez.no / exponential / documentation / development / libraries / ez template / functions / section
These documentation pages are no longer maintained. Please visit the new documentation site.
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.
Defines the namespace for all generated template variables, see loop and sequence for a list of generated variables.
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.
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.
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.
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.
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.
Determines the start of the alternative block which is shown when show is false. See above.
Determines a block of template elements which should be placed in between two iterations.
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.
<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}
For each iteration you can see the template variables
which are set by the section function, they are index:number:key
0:1:2 Number: 1003
0:1:red Text: Red
1:2:green Text: Green
2:3:blue Text: Blue
red-0:1:0 Number: 1
blue-1:2:2 Number: 3
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
|
URI |
Name |
|
odd - http://ez.no |
eZ home |
|
even - http://zez.org |
ZeZ |
|
odd - http://.ez.no/developer |
eZ developer |
Show list=off
Shown for zero or empty vars
1.2.3.4.5
0::-1::-2::-3::-4
Comments
How to use section show with different datatypes
Helle Andersen
Friday 15 July 2005 9:47:58 am
* XML text attribute:
* Text line, images and URL attributes:
* Matrix attribute
* Time-date field
E.g. 12.40 - 12.45
A padding column
Norman Leutner
Friday 10 June 2005 6:18:38 pm
This adds a padding colums after each TD and a TR after five TD`s...
if-then-else statements
Frey Staso
Thursday 31 March 2005 8:37:07 pm
{let usernum=5}
{section name=if show=eq($usernum, 5)}
usernum equals 5
{/section}
{section-else}
usernum does not equal 5
{/section}
The nice part about this code, is whatever you put before {/section} will not show up unless $usernum = 5. So you can put HTML code in there and only some people can see a picture or something. Hope this helps someone!
Different results on different platforms?
Kristof Coomans
Friday 07 May 2004 2:46:54 pm
A padding column
Eirik Johansen
Friday 30 April 2004 9:23:33 am
<table border="1">
<tr>
{section var=item loop=10}
<td>Cell nr {$item}
</td>
{delimiter modulo=1}
{section show=$item.number|mod(2)|eq(0)}
<td> </td>
{section-else}
</tr>
<tr>
{/section}
{/delimiter}
{/section}
</tr>
</table>
Sincerely,
Eirik Johansen
how to display info from a database to a <select> using {section and loop}
javier sanchez
Saturday 06 March 2004 10:47:50 pm
table = (country | city)
i fetch the db, an array return as result, this array will have field names as keys pointing to arrays of data, my main problem is how i loop elements from an array, with different keys
my code is something like this:
$elements == resulting array from db fetch
<select name="sel">
{section name=seccion loop=$elements}
<option value="{$seccion:item.city}">{$seccion:item.country}</option>
{/section}
</select>
aany help on this topic or in "how to loop an bi-dimensional array with keys" will be great
thx, javier sanchez-galan