REPEAT
This element is a static repeat loop. It can be used to repeat several times the same piece of code, with changes managed by an index variable for each instance. There are several ways to define the index for the loop, either with numbers or with a list of strings.
Attributes brief detailed show all inherited
Name | Description | Default | |
---|---|---|---|
index_name | name of the variable used for the index in the loop | index | |
count | number of repeats | 0 | |
start | start value for the index published in the repeat loop | 0 | |
index_list | List of values for the index variable. Also implies the number of repeats | empty |
Examples
The following code:
- <REPEAT count="2" start="1">
- <ELEMENTA attribute_a="$index$"/>
- </REPEAT>
Is equivalent to:
- <ELEMENTA attribute_a="1"/>
- <ELEMENTA attribute_a="2"/>
Similarly, using the index_list attribute instead, the following code:
- <REPEAT index_list="A;B">
- <ELEMENTA attribute_a="$index$"/>
- </REPEAT>
Is equivalent to:
- <ELEMENTA attribute_a="A"/>
- <ELEMENTA attribute_a="B"/>
And you can modify the index_name attribute for nested loops, so that the following code:
- <REPEAT count="2" start="1">
- <REPEAT index_name="sub_index" count="2" start="1">
- <ELEMENTA attribute_a="$index$-$sub_index$"/>
- </REPEAT>
- </REPEAT>
Is equivalent to:
- <ELEMENTA attribute_a="1-1"/>
- <ELEMENTA attribute_a="1-2"/>
- <ELEMENTA attribute_a="2-1"/>
- <ELEMENTA attribute_a="2-2"/>
A formula can be used to compute the index and start values. For example the following code:
Is equivalent to:
- <ELEMENTA attribute_a="2"/>
- <ELEMENTA attribute_a="3"/>
Advanced usage
Sometimes you need to increase numeric index as well as loop through the strings in the index_list. You can achieve this using additional VARIABLE with override set to "true". Additionally we can shorten the code using some defines (already defined in LM Skin).
- <DEFINE>
- <VAR base_type="VARIABLE" override="true" />
- <R base_type="REPEAT" />
- </DEFINE>
- <VAR id="N" value="0" />
- <VAR id="list" value="Apples;Peaches;Oranges" />
- <VAR id="reversed_list" value="" />
- <R index_list="$list$">
- <TEXT value="$N$: $index$" />
- <VAR id="N" formula="$N$+1" />
- <VAR id="reversed_list" value="$index$;$reversed_list$" />
- </R>
- <TEXT value="$reversed_list$" />
REPEAT as "IF"
Another very useful thing is ability to use formulas in "count" attribute. True = 1, false = 0.
However, you can use only numeric and boolean values in formulas. If you need to compare strings, you can use build time scripting possibilities for VARIABLE. Or something! 234