• News
  • Developer information
  • Projects & contributions
  • Forum
  • Issue tracker
  • Open Funding
  • Security
  • User groups
  • Support
  • Store
  • Company

  • Path

    ez.no / exponential / documentation / development / libraries / ez template / functions / section


    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.

    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 - 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

    Loop 5 times

    1.2.3.4.5

    Loop 5 times negative

    0::-1::-2::-3::-4

    Comments

    How to use section show with different datatypes

    Note that different code is used for different datatypes (it would be a good idea to change this in the future releases ...):

    * XML text attribute:
    
    
    
    {section show=$node.object.data_map.xxx.content.is_empty|not}
    
    
     
    
    <h1>xxx:</h1>
    
    
    {attribute_view_gui attribute=$node.object.data_map.xxx}
    
    
    {/section}
    
    
    


    * Text line, images and URL attributes:
    
    
    
    {section show=$node.object.data_map.xxx.content}
    
    
    xxx: {attribute_view_gui attribute=$node.object.data_map.xxx}
    
    
    {/section}
    
    
    


    * Matrix attribute
    
    
    
    {section show=$node.object.data_map.xx.content.rows.sequential}
    
    
    {attribute_view_gui attribute=$node.object.data_map.xx}
    
    
    {/section}
    
    
    


    * Time-date field

    E.g. 12.40 - 12.45
    
    
    
    {section show=$node.object.data_map.xxx.content.is_valid} - {attribute_view_gui attribute=$node.object.data_map.xxx}
    
    
    {/section}
    
    
    

    A padding column

    You can also add a pinnd column without using the delimeter by:
    
    
    
    <table border="1">
    
    
     <tr>
    
    
             {section var=item loop=10}
    
    
                    <td>
    
    
                            Cell
     nr: {$:item.number}
    
    
                    </td>
    
    
     
    
                    
    {section show=$:item.number|mod(5)|eq(0)}
    
    
                     </tr>
    
    
                     <tr>
    
    
                    {section-else}
    
    
                     <td>&nbsp;</
    td>
    
    
                    {/section}
    
    
             {/section}
    
    
     </tr>
    
    
    </table>
    
    
    


    This adds a padding colums after each TD and a TR after five TD`s...

    if-then-else statements

    For anyone out there that needs to do an if statment:


    {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?

    On Windows XP {section show=$emptyArray}test{/section} where $emptyArray is an empty array, the word "test" will not display. But on Linux, it is displayed, regardless of the empty array $emptyArray.

    A padding column

    Here's how one might add a padding column to a two-columned table:

    <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>&nbsp;</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}

    i want to display contents from a table which has 2 fields, each row will be an <option> of the <select>

    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

    Contents

    Development

    Extensions
    Exponential datamodel
    Exponential tuning and stability
    Importing attribute data
    Kernel
    Libraries
        eZ xml
        eZ db
        eZ i18n
        eZ soap
        eZ template
           Basics
           Functions
              Function list
              Section
              Sequence
              Delimiter
              Switch
              Include
           Operators
        eZ webdav
    Scripting
    Standards
    System overview
    Test Suite
    Using Doxygen to create API documenta...


    Created

    07/07/2003
    3:12:01 pm
    by Bård Farstad

    Last updated

    07/07/2003
    3:16:07 pm
    by Bård Farstad

    Authors

    Bård Farstad



    This page is part of the Exponential documentation. The documentation is available under the GNU Free Documentation License. All contributions will be released under the terms of this license.