Caution: This documentation is for eZ Publish legacy, from version 3.x to 5.x.
For 5.x documentation covering Platform see eZ Documentation Center, for difference between legacy and Platform see 5.x Architecture overview.

Control structures

The eZ publish template language offers a selection of mechanisms that can be used to solve common programmatic issues like for example condition control, looping, etc. The following list shows an overview of the available mechanisms:

  • IF-THEN-ELSE
  • SWITCH
  • WHILE
  • DO...WHILE
  • FOR
  • FOREACH

IF-THEN-ELSE

The IF construct allows for conditional execution of code fragments. It is one of the most important features of many programming languages. The eZ publish implementation makes it possible to do conditional branching by the way of the following elements: IF, ELSE and ELSEIF. The ELSE and ELSEIF elements are optional. The following examples demonstrate the use of this construct.

Example 1

{if eq( $var, 128 )}
    Hello world <br />
{else}
    No world here, move along. <br />
{/if}

Example 2

{if eq( $fruit, 'apples' )}
    Apples <br />
{elseif eq( $fruit, 'oranges' )}
    Oranges <br />
{else}
    Bananas <br />
{/if}

SWITCH

The SWITCH mechanism is similar to a series of IF statements used on the same expression. This construct is typically useful when the same variable needs to be compared to different values. It executes a piece of code depending on which value that matched a given criteria. The following example demonstrates basic use of this construct.

{switch match=$fruits}
 
    {case match='apples'}
        Apples <br />
    {/case}
 
    {case match='oranges'}
        Oranges <br />
    {/case}
 
    {case}
        Unidentified fruit! <br />
    {/case}
 
{/switch}

If the value of the $fruits variable is "oranges", the following output will be produced:

Oranges

WHILE

The WHILE construct is the simplest loop mechanism that the template language offers. It tells eZ publish to execute the nested statement(s) repeatedly, as long as a given expression evaluates to TRUE. The value of the expression is checked for every loop iteration (at the beginning of the iteration). If the given expression evaluates to FALSE from the very beginning, the nested statement(s) will not be executed. The following example demonstrates basic use of this construct.

{while ne( $counter, 8 )}
 
    Print this line eight times ({$counter}) <br />
    {set $counter=inc( $counter )}
 
{/while}

If the initial value of $counter is zero, the following output will be produced:

Print this line eight times (0)
Print this line eight times (1)
Print this line eight times (2)
Print this line eight times (3)
Print this line eight times (4)
Print this line eight times (5)
Print this line eight times (6)
Print this line eight times (7)

DO...WHILE

A DO...WHILE loop is very similar to WHILE loops, except that the expression is checked at the end of each iteration instead of in the beginning. The main difference is that this construct will always execute the first iteration (regardless of how the test expression evaluates). The following example demonstrates basic use of this construct.

{do}
 
    Keep printing this line ({$counter}) <br />
    {set $counter=inc( $counter )}
 
{/do while ne( $counter, 8 )}

If the initial value of $counter is 0, the following output will be produced:

Keep printing this line (0)
Keep printing this line (1)
Keep printing this line (2)
Keep printing this line (3)
Keep printing this line (4)
Keep printing this line (5)
Keep printing this line (6)
Keep printing this line (7)
Keep printing this line (8)

FOR

Generic looping may be achieved using FOR loops. This construct supports looping over numerical ranges in both directions. In addition it also supports breaking, continual and skipping. The following example demonstrates basic use of this construct.

{for 0 to 7 as $counter}
 
    Value of counter: {$counter} <br />
 
{/for}

The following output will be produced:

Value of counter: 0
Value of counter: 1
Value of counter: 2
Value of counter: 3
Value of counter: 4
Value of counter: 5
Value of counter: 6
Value of counter: 7

FOREACH

The FOREACH construct can be used to iterate over arrays in different ways. The loop can be tweaked using miscellaneous techniques. The following example demonstrates basic use of this construct.

{foreach $objects as $object}
 
    {$object.name} <br />
 
{/foreach}

The example above will print out the names of the objects that are stored in the $objects array. If this array stores 4 objects with the following names: "Emmett Brown", "Marty McFly", "Lorraine Baines" and "Biff Tannen", the following output will be produced:

Emmett Brown
Marty McFly
Lorraine Baines
Biff Tannen

Balazs Halasy (16/02/2005 10:35 am)

Balazs Halasy (26/04/2005 9:50 am)


Comments

There are no comments.