switch

Summary

Allows conditional control of output.

Usage

{switch [name=value1] [var=value2]
        match=value3}
[{case match=value4}
   ...tpl code
{/case}]
[...]
{case}
   ...tpl code
{/case}
{/switch}

Parameters

NameTypeDescriptionRequired
name mixed Namespace for all generated template variables
match(=value3) mixed Specify which variable we want to compare with
match(=value4) mixed Match criteria
var(=value2) mixed Name of the created variable which contains the match value (Exponential 3.4 and later only)

Returns

Description

The switch function allows conditional control of output. For instance you can display a piece of HTML code depending on a template variable. The matching can be directly between two types or matching on an element in an array.

The matching is done by creating one or more case blocks inside the switch block. There must always be one default case present in Exponential before version 3.4, a default case is created by inserting a case block without any parameters.

The parameter to a case can either be match which determines the value to match against, or in which must contain an array. The match does a direct match, while the in looks for a match among the elements in the array. The in parameter behaves differently if the key parameter is used, which must be an identifier, it then assumes that the array sent to in has an array for each element and uses the key to match a key in the sub array.

name
Defines the namespace for all generated template variables. The switch will create a variable named "match". So if you are nesting switch blocks, you have to define a namespace in order to hold the "match" variables apart. If you don't nest blocks, you don't need to use name. In Exponential 3,4 and later you can use the optional "var" argument to rename the created variable so that you are not required to use namespaces anymore.

note:
Exponential versions before 3.4 do not support the use of "var" instead of "name" for this functions, like section does.

Examples

example
A simple exmple. Since $myvar equals '1', the code inside the {case match=1} block will be run.

{let myvar=1 message='welcome' }

message : {$message} <br/>

 

{switch match=$myvar}

 {case match=1}

 This one matches. <br/>

 match : {$match}<br/>

 my message : {$message}<br/>

 {/case}

 

 {case match=2}

 This one does not match.

 {/case}

 

 {case}

 Not this one either.

 {/case}

{/switch}

{/let}

example
The same example as the previous one, except that we now use a namespace

{let myvar=1 message='welcome' }

message : {$message} <br/>

 

{switch name=Sw1 match=$myvar}

 {case match=1}

 This one matches. <br/>

 match (addressing with "current" namespace) : {$:match}<br/>

 match (addressing with absolute namespace) : {$Sw1:match}<br/>

 my message : {$message}<br/>

 {/case}

 

 {case match=2}

 This one does not match.

 {/case}

 

 {case}

 Not this one either.

 {/case}

{/switch}

{/let}

example
Here we use namespaces and nested switch blocks.

{let myvar1=1 myvar2=3 message='welcome' }

message : {$message} <br/>

 

{switch name=Sw1 match=$myvar1}

{case match=1}

   ====== OUTPUT OF FIRST SWITCH START =====<br/>

   This one matches. <br/>

   match (addressing with "current" namespace) : {$:match}<br/>

   match (addressing with absolute namespace) : {$Sw1:match}<br/>

   my message : {$message}<br/>

 

   {switch name=Sw2 match=$myvar2}

   {case match=2}

       This one does not match.<br/>

   {/case}

 

   {case match=3}

       ====== OUTPUT OF SECOND SWITCH START =====<br/>

       This one matches. <br/>

       match (addressing with "current" namespace) : {$:match}<br/>

       match (addressing with absolute namespace) : {$Sw1:Sw2:match}<br/>

       match in namespace Sw1 : {$Sw1:match}<br/>

       my message : {$message}<br/>

       ====== OUTPUT OF SECOND SWITCH STOP =====<br/>

   {/case}

 

   {case}

       Not this one either.<br/>

   {/case}

   {/switch}

   ====== OUTPUT OF FIRST SWITCH STOP =====<br/>

{/case}

 

{case match=2}

   This one does not match.<br/>

{/case}

 

{case}

   Not this one either.<br/>

{/case}

{/switch}

{/let}

The output of the example is:

 message : welcome 

 ====== OUTPUT OF FIRST SWITCH START =====

 This one matches. 

 match (addressing with "current" namespace) : 1

 match (addressing with absolute namespace) : 1

 my message : welcome

 ====== OUTPUT OF SECOND SWITCH START =====

 This one matches. 

 match (addressing with "current" namespace) : 3

 match (addressing with absolute namespace) : 3

 match in namespace Sw1 : 1

 my message : welcome

 ====== OUTPUT OF SECOND SWITCH STOP =====

 ====== OUTPUT OF FIRST SWITCH STOP =====

Comments

Contents

Reference

Database diagram
Template operators
Template functions
    GUI
    Miscellaneous
    Program flow
       section
       switch
    Variables
Data fetching
Modules
XML tags


Created

06/02/2004
2:21:41 pm
by Balazs Halasy

Last updated

17/10/2004
11:42:35 am
by Luc Chase

Authors

Balazs Halasy
Vidar Langseid
Derick Rethans
Luc Chase



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.