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

switch

Summary

Allows conditional control of code execution.

Usage

{switch match=<variable>}

    {case match=<value>} 
    {case in=<array>}
    ...
    {/case}

    {case}
    ...
    {/case}

{/switch}

Description

This 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. A default case should always be provided.

Please note that it is also possible to match inside arrays. This can be done by making use of the "in" argument, it is demonstrated in the last (third) example.

Examples

Example 1

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

The following output will be produced: "Oranges".

Example 2

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

The following output will be produced: "Unidentified fruit!" - which is the outcome of the default case (none of the other cases matched).

Example 3

{def $digit=1}
 
{switch match=$digit}
 
    {case in=array( 1, 2 )}
        This one matches.
    {/case}
 
    {case in=array( 2, 3 )}
        This one does not match.
    {/case}
 
    {case}
        Not this one either.
    {/case}
 
{/switch}

The following output will be produced: "This one matches.".

Balazs Halasy (22/02/2005 1:09 pm)

Balazs Halasy (08/11/2005 12:34 pm)


Comments

  • This is not a switch

    A switch is not only a serie of if. There is fallbacks. With this syntaxe, how do we not write 'break;' to make the action continue through other case blocks?
    • Re: This is not a switch

      I think the template language is too primitive at this point to able to support that.. Not that I've looked through ez's parsing system but if you look through it and can understand syntax parsing in general you'll be able to find a hack yourself (if possible without warnings or errors) or maybe even the way to do what you want without much hacking.