if(...)
The if(...) function can be used to check a condition (in reduced form). This is useful, for example, to check whether a link text has been set or not when maintaining links. If no link text is set, the default text “Link” will be output (cf. also 4. Example)).
The if(...) function can be used within a $CMS_VALUE(...)$ instruction. |
The general syntax of if(...) is:
$CMS_VALUE(
if(
CONDITION_1,
CONDITION_TRUE_1,
CONDITION_2,
CONDITION_TRUE_2,
...,
CONDITION_N,
CONDITION_TRUE_N,
CONDITION_FALSE
)
)$
Specification of a condition (CONDITION_1) and an execution part for fulfilled condition (CONDITION_TRUE_1), is mandatory information. All other details are optional. |
CONDITION
To define a condition (CONDITION) which can be used in the if(...) function, a maximum of three parameters are required:
- Variable or constant
- Operator (optional)
- Comparable variable, comparable constant or reference value (optional)
The expression:
1 == 1
is therefore a valid condition as “1” is a valid constant, “==” is a valid operator and “1” is a valid comparable constant.
This also applies accordingly to the following expression:
a != b
The value of a variable or constant can be compared with another value via a condition or an operator respectively (see expressions). This is useful, e.g. if the value of the variable a changes within a function (e.g. Navigation); however, the comparable value is constant in several sub-areas.
In addition, there are several methods in FirstSpirit which return a Boolean value (e.g. #global.preview). For expressions which return a Boolean value, it is not necessary to specify an Operator or a Comparable value .
$CMS_VALUE(
if(
#global.preview,
"EXECUTION PART (PREVIEW)",
"EXECUTION PART (DEPLOYMENT)"
)
)$
Information about the operator precedence within expressions are given in the Chapters logical expressions and comparative expressions. |
Evaluation
The if(...) function checks whether a given condition is “true” or “false”. The “true” branch is separated from the condition by a comma.
$CMS_VALUE(
if(
a != b,
"That is my output!"
)
)$
However, in the code example given above an output is only generated if the condition is “true”.
If a result is to be output even if the condition is “false” a “false” branch is required which is separated from the “true” branch by a comma.
$CMS_VALUE(
if(
a != b,
"That is my output!",
"That is not correct!"
)
)$
If a = 1 and b = 2 the condition is true and the function outputs the text “That is my output!”.
If a = 1 and b = 1 the condition is false and the function outputs the text “That is not correct!”.
Further, any number of conditions/“true” branches can be specified, however, only one “false” branch. If none of the conditions applies the “false” branch is output (if available). If a condition applies (is true) the corresponding “true” branch is output.
$CMS_VALUE(
if(
a != b,
"That is my output!",
a != c,
"That is another output!",
"That is not correct!"
)
)$
If a=1 and b=2 and c=1 the first condition applies and the function outputs the text “That is my output!”.
If a=1 and b=1 and c=2 the second condition applies and the function outputs the text “That is another output!”.
If a=1 and b=1 and c=1 none of the two conditions apply and the function outputs the text “That is not correct!”.
Examples of if(...)
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.
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: Output of the change date in the preview
Code example:
$CMS_VALUE(
if(
#global.preview,
"Change date: " + #global.page.changeDate.format("yyyy-MM-dd HH:mm")
)
)$
Description:
The example outputs the change date of a page of the Page Store in a preview.
Output:
Change date: 2007-02-19 10:39
or the change date in ISO format
2nd Example: Comparison of two constants
Code example:
$CMS_SET(set_var1,"A")$
$CMS_SET(set_var2,"B")$
$CMS_VALUE(
if(
set_var1 == set_var2,
"A == B",
"A != B"
)
)$
Description:
In the example, two constants set_var1 and set_var2 are defined. The if(...) function compares set_var1 with set_var2.
Output:
As the content of set_var1 differs from the content of set_var2 the result is:
A != B
3rd Example: Modulo check in a loop
Code example:
$CMS_SET(set_compare)$
$CMS_VALUE(
if(
(for_val % 2) == 0,
"even",
"odd"
)
)$
$CMS_END_SET$
$CMS_FOR(for_val, [0 .. 10])$
The number "$CMS_VALUE(for_val)$" is $CMS_VALUE(set_compare)$
$CMS_END_FOR$
Description:
The example describes the dynamic modulo check within a loop.
First, a document fragment set_compare is defined with the help of $CMS_SET(...)$...$CMS_END_SET$. In this fragment, depending on the given value, a modulo check determines whether the value is “even” or “odd”.
The values are specified by $CMS_FOR(...)$.
The given $CMS_FOR(...)$ loop invokes the fragment for each integer from 0 to 10.
Output:
The number "0" is even
The number "1" is odd
The number "2" is even
The number "3" is odd
The number "4" is even
The number "5" is odd
The number "6" is even
The number "7" is odd
The number "8" is even
The number "9" is odd
The number "10" is even
Alternatively, this can also be realised with the method: even():
$CMS_SET(set_compare)$
$CMS_VALUE(
if(
for_val.even,
"even",
"odd"
)
)$
$CMS_END_SET$
$CMS_FOR(for_val, [0 .. 10])$
The number "$CMS_VALUE(for_val)$" is $CMS_VALUE(set_compare)$
$CMS_END_FOR$
and without document fragment:
$CMS_FOR(for_val, [0 .. 10])$
The number "$CMS_VALUE(for_val)$" is $CMS_VALUE(
if(
for_val.even,
"even",
"odd"
)
)$
$CMS_END_FOR$
4th Example: Check whether a link field has been set
Code example:
<a href="$CMS_REF(lt_reference)$">
$CMS_VALUE(
if(
!lt_text.isEmpty, lt_text, "Link"
)
)$
</a>
Description:
lt_text is a field for storing the link text of a link. The example output the entered text if the field is not empty. If the field is empty, the default text “Link” will be output (cf. also the Data type String ).