The loop directive includes two built-in variables that you can use for embedded if
conditions. The variables are: _length
and _index
. The _length
variable evaluates to the length of the arrays processed by the loop directive, while the _index
variable evaluates to the current index of the 'loop' directive. To ensure that the variables are only recognized as directives, and not as actual parameters to be passed into the loop, do not enclose either variable in @@
s.
An example of using built-in variables is to apply them to the import
attribute of the page directive. The import
attribute requires comma separation of packages. If the loop
directive extends around the entire import
attribute, you would only output the attribute name import=
on the first iteration of the loop--this includes the closing double quote (")--and not output a comma on the last iteration of the loop. Using the built-in variable, you can express this as follows:
<@loop (@@Import@@)@> <@ if(_index == 0)@>import=" <@endif@>@@Import@@<@if (_index == _length-1)@>"<@else@>, <@ endif @> <@endloop@>