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

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

Using $CMS_SWITCH(...)$

Contents

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

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”.

2nd example: Class dependent output ("class")

Code example:

$CMS_SWITCH(_var)$
Unknown
$CMS_CASE(class("java.math.BigInteger"))$
Integer
$CMS_CASE(class("java.lang.String"))$
String
$CMS_END_SWITCH$

Description:
The output can be made dependent of the class of the object which is indicated by means of $CMS_SWITCH(...), too. The class name is used for this, for example

class("java.math.String")    or
class("java.math.BigInteger")

This corresponds to the Java construct “instanceof” (type checking, for example var instanceof java.lang.String).

If “_var” is a string, for example

$CMS_SET(_var, "ABC")$

then the output will be

String

If it is an integer, for example

$CMS_SET(_var, 123)$

then the output will be

Integer

This check can be carried out more abbreviate by means of isCase, for example

$CMS_IF(class("java.lang.String").isCase(_var))$
String
$CMS_END_IF$

© 2005 - 2024 Crownpeak Technology GmbH | All rights reserved. | FirstSpirit 2024.12 | Data privacy