Sections

These documentation pages are no longer maintained. Please visit the new documentation site.

Sections and looping

It is often necessary to repeat elements of the template. This is very useful in lists of objects. By doing this we can define how we wish to present one object in our list, and then repeat it for each object. We do this with sections.

Example:

{section name=MySection loop=$objectList}

   <!-- do things here -->

{/section}

This section would loop the same number of times as there is elements in $objectList. If the array $objectList is empty then this section will be ignored. Remember also to close the section with {/section}.

Each section must have a name. The name will declare the unique namespace for this section. To access a variable in the namespace you have to add the name of the namespace to the variable.

Example:

{$MySection:myvariable}

For each loop of the section we will take the next element of $objectList and place this in a variable called $item. This variable will change for each time the section loops.

Example:

{section name=MySection loop=$objectList}

   {$MySection:item.name}<br />

{/section}

This example will print out the name of each element in the $objectList array. We can reach the data map in the same way:

{section name=MySection loop=$objectList}

   <h1>{$MySection:item.data_map.title.content}</h1>

   {$MySection:item.data_map.intro.content.output.output_text}<br />

{/section}

Showing or hiding a section

A section can take another parameter called show. The parameter can be true or false and decides if the section should be shown or not. If the parameter is an array, it will be shown if the array has values and hidden if the array is empty.

{section name=NewSection show=$node.node_id|eq(2)}

   <!-- more stuff -->

{/section}

This section will only be shown if the $node object has id 2 (eq is an equal operator and return true or false if the input and parameter are equal or not).

Example:
If we wish to print out the name of all nodes placed under the current position in the tree it could be something like this:

{let articleList=fetch( 'content', 'list', hash( 'parent_node_id', $node.node_id,

                                                'sort_by', $node.sort_array ) )}

{section name=articleLoop loop=$articleList}

   {$articleLoop:item.name}

{/section}

{/let}

We are, as you can see, sorting the list by the sort_array variable in the node. This will ensure that it will be sorted the way that is decided by the parent node. Notice that we use a colon after the namespace. This is because articleLoop is a namespace and not an object.

Alternately we can cut down a bit on the code:

{section name=articleLoop loop=fetch( 'content', 'list',

                                     hash( 'parent_node_id', $node.node_id,

                                           'sort_by', $node.sort_array ) )}

   {$articleLoop:item.name}

{/section}

Here we place the fetch function directly into the section. This will save us a few lines of code, but we don't have a name on the list, and can therefore not use it in any other way.

Comments

Use 'var' parameter instead of 'name'

From http://ez.no/products/ez_publish_...reference/data_fetching/content/list :
"
It is preferred to use var instead of name. var is only supported in eZ publish 3.3 and later.
"

Dont mix up term section with section

The section term could easily be mistaken for the other term Section which is used for creating a different view/pagelayout on the same site. Eg. placing a pagelayout_section_1.tpl pagelayout_section_2.tpl etc. in the override/template/ -folder of ezpublish. (if you are a newbie at least)

regards,
claÜs

Contents

Customization

Access control
eZ publish API Documentation
Content structure
Custom design
    User specified parameters
    Template variables set by ezPublish
    Introduction
    Variables, arrays and objects
    Node
    Database connectivity
    Sections
    File placement
    Override templates
    node_view_gui
    Pagination
    Caching
    Stylesheets
    Pagelayout
    Printable pages
    Examples
Components
Tips & Tricks
Troubleshooting


Created

04/07/2003
11:28:02 am
by Bård Farstad

Last updated

08/07/2003
1:19:33 pm
by Bård Farstad

Authors

Bård Farstad



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