Start page
Start page

Start page / Template development / Template syntax / Instructions / $CMS_SWITCH

$CMS_SWITCH(...)$Available from FirstSpirit Version 4.0

Using $CMS_SWITCH(...)$

Within a template, depending on a condition, for example the width of a picture, different outputs can be generated in the corresponding position of a page.
Conditional evaluations are carried out using the following instructions:

  • $CMS_IF(...)$
  • $CMS_SWITCH(...)$

Unlike the instruction $CMS_IF(...)$, in $CMS_SWITCH(...)$ one variable is checked for several possible contents (case differentiation). Depending on the specified content, another result can be output.

Syntax of $CMS_SWITCH(...)$

The basic structure of a loop with $CMS_SWITCH(...)$ is as follows:

$CMS_SWITCH(OBJECT)$
STANDARDEXECUTIONPART
$CMS_CASE(CONDITION_1)$
EXECUTIONPART_1
$CMS_CASE(CONDITION_2)$
EXECUTIONPART_2
...
$CMS_CASE(CONDITION_N)$
EXECUTIONPART_N
$CMS_END_SWITCH$

An OBJECT is passed to the $CMS_SWITCH(...)$ instruction for being checked.

Expressions up to the first $CMS_CASE(...)$ block represent the standard case (default).

It is followed by any $CMS_CASE(...)$ blocks with a CONDITION and an EXECUTIONPART.

A $CMS_SWITCH(...)$ instruction is evaluated as follows:

The $CMS_CASE(...)$ blocks are compared one after the other with the given CONDITIONS against the value of OBJECT .

If the CONDITION matches the value of OBJECT the EXECUTIONPART of this $CMS_CASE(...)$ block is executed.

If the two values do not match the next $CMS_CASE(...)$ block is used for the comparison.

If the CONDITION of none of the $CMS_CASE(...)$ blocks matches the value of the OBJECT the STANDARDEXECUTIONPART is executed.

A $CMS_SWITCH(...)$ instruction can therefore also be rewritten as a $CMS_IF(...)$ instruction:

$CMS_SWITCH("value")$
default output
$CMS_CASE("no_value")$
No value
$CMS_CASE("value")$
Value
$CMS_END_SWITCH$

is the equivalent to:

$CMS_IF("no_value".equals("value"))$
No value
$CMS_ELSEIF("value".equals("value"))$
Value
$CMS_ELSE$
default output
$CMS_END_IF$

Parameters of $CMS_SWITCH(...)$

$CMS_SWITCH(...)$ has one parameter only: an OBJECT.

OBJECT

To define a $CMS_SWITCH(...)$ instruction it is absolutely necessary to specify an object:

$CMS_SWITCH("value")$
...
$CMS_END_SWITCH$

or

$CMS_SWITCH("varName")$
...
$CMS_END_SWITCH$

or

$CMS_SWITCH(varName.toString)$
...
$CMS_END_SWITCH$

The object can be both a constant as well as another variable or an Expression .

Important Information about the operator precedence within expressions are given in the Chapters logical expressions and comparative expressions.

Parameters of $CMS_CASE(...)$

The $CMS_CASE(...)$ blocks also have one parameter: the CONDITION.

CONDITION

A condition can be both a constant as well as another variable or an Expression .

Examples of $CMS_SWITCH(...)$

Several examples of use of the instruction within templates are shown in the following. The examples are intended to clearly show the specific effect of the instruction and provide help for the template developer when creating their own templates.

Important The examples displayed here must be adjusted for use within a project! For example, variable names must be changed to the specific variable names of the project in which the instruction is to be used.

1st Example: Language-dependent output

Code example:

$CMS_SWITCH(#global.language.abbreviation.toLowerCase)$
Welcome!
$CMS_CASE("de")$
Willkommen!
$CMS_CASE("en")$
Welcome!
$CMS_CASE("fr")$
Bienvenue!
$CMS_END_SWITCH$

Description:

In a preview the example outputs "Willkommen!" for the language with the abbreviation "de", "Bienvenue!" for "fr", and "Welcome!" for "en" and all other languages.

Output:

Willkommen!

for the language with the abbreviation "de".

© 2005 - 2014 e-Spirit AG | All rights reserved. | Last change: 2010-12-15