$CMS_FOR(...)$
Using $CMS_FOR(...)$
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(...)$
Alternatively, for the first time ever in FirstSpirit 4.0, loops can also be realised. The instruction $CMS_FOR(...)$ can be used for this.
The intended uses of loops are:
- Output of lists and complex elements
- Iterative executions of instructions
The instruction $CMS_FOR(...)$ replaces the iterative function of $CMS_RENDER(...)$! |
Syntax of $CMS_FOR(...)$
The basic structure of a loop with $CMS_FOR(...)$ is as follows:
$CMS_FOR(IDENTIFIER, OBJECT)$
LOOP BODY
$CMS_END_FOR$
Within the loop body, in addition to the object with the variable name IDENTIFIER the system object #for is also available to the user.
Parameters of $CMS_FOR(...)$
The instruction $CMS_FOR(...)$ has two parameters.
The first parameter IDENTIFIER is a variable name for use in the body of the loop (LOOP BODY).
The second parameter OBJECT is a set-valued element, e.g. a list or a number set or, in general an Expression .
IDENTIFIER
A Variable name is to be given for the identifier.
During the execution of $CMS_FOR(...)$ an object with this identifier is then available which represents the current object (in the list).
OBJECT
For the parameter OBJECT a Variable, Constant expression etc. can be used.
A number set, List or Map is usually given.
A number set is enclosed in "[" and "]". The start and end value are separated from each other by two dots:
$CMS_FOR(IDENTIFIER, [STARTVALUE .. ENDVALUE])$
LOOP BODY
$CMS_END_FOR$
The two values must be integers. Alternatively a variable with a number set can be used.
Several values can be grouped together to form a List . Within the list the values can be iterated:
$CMS_FOR(IDENTIFIER, [OBJEKT_1, OBJEKT_2, ..., OBJEKT_N])$
LOOP BODY
$CMS_END_FOR$
In addition, by grouping value pairs together to form a Map it is also possible to iterate over the elements:
$CMS_FOR(IDENTIFIER, {SCHLÜSSEL_OBJEKT_1 : WERT_OBJEKT_1, ..., SCHLÜSSEL_OBJEKT_N : WERT_OBJEKT_N})$
LOOP BODY
$CMS_END_FOR$
Further information on the use of expressions for OBJECT are given in the sub-chapter Constants.
Examples of $CMS_FOR(...)$
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 template developers 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: Lists
Code example:
$CMS_FOR(for_wrapper, [1, 3, 5, 7, 9, 11])$
Index $CMS_VALUE(#for.index)$: Value $CMS_VALUE(for_wrapper)$<br>
$CMS_END_FOR$
Description:
In the example given a loop is defined which is run through the given manual list six times.
During a run through the list the system object #for is used to output the number of the run (counting from 0) and the value of the object for_wrapper (during the run).
Output:
Index 0: Value 1
Index 1: Value 3
Index 2: Value 5
Index 3: Value 7
Index 4: Value 9
Index 5: Value 11
2nd Example: number set
Code example:
$CMS_FOR(for_wrapper, [7 .. 14])$
Index $CMS_VALUE(#for.index)$: Value $CMS_VALUE(for_wrapper)$<br>
$CMS_END_FOR$
Description:
In this example the integer number set from 7 up to and including 14 is defined. The loop is then run through once for each integer.
As in the previous example the run and the value are output.
Output:
Index 0: Value 7
Index 1: Value 8
Index 2: Value 9
Index 3: Value 10
Index 4: Value 11
Index 5: Value 12
Index 6: Value 13
Index 7: Value 14
3rd Example: Map
Code example:
$CMS_FOR(for_wrapper, {"k_1" : "v_1", "k_2" : "v_2", "k_3" : "v_3", "k_4" : "v_4"})$
Index $CMS_VALUE(#for.index)$:<br>
Key $CMS_VALUE(for_wrapper.key)$ , value $CMS_VALUE(for_wrapper.value)$<br><br>
$CMS_END_FOR$
Description:
A map is defined in the example. The number of the run as well as the key name as well as the value of the key is output in the loop.
Output:
Index 0:
Key k_1 , Value v_1
Index 1:
Key k_2 , Value v_2
Index 2:
Key k_3 , Value v_3
Index 3:
Key k_4 , Value v_4