Loop variable builtins
These built-ins you can only use with the loop variable of the list
and items
directives. Some explanation of that follows (loopVar?index
returns the 0-based index in the listable value we iterate through):
When the list
directive doesn’t specify the loop variable, these built-ins are used with the loop variable of the items directive:
Loop variable built-ins only use the name of loop variable, so that they can identify the related ongoing iteration. They don’t read the value of the loop variable. Hence, this is a parsing error:
counter
Returns the 1-based index where the iteration (which is identified by the loop variable name) currently stands.
For the 0-based index, use the
index
built-in.
has_next
Tells if the item where the iteration (which is identified by the loop variable name) currently stands is not the last item.
For separating items with commas and such, use
<#sep>separator</#sep>
instead of<#if var?has_next>separator</#if>
, as it’s more readable. (Furthermore the</#sep>
can be often omitted, like in<#list ... as var>...${var}...<#sep>separator</#list>
)
If you need the inverse of this built-in, use
var?is_last
instead of!var?has_next
, because it’s more readable.
index
Returns the 0-based index where the iteration (which is identified by the loop variable name) currently stands.
For the 1-based index, use the
counter
built-in.
is_even_item
Tells if the item where the iteration (which is identified by the loop variable name) currently stands has an even 1-based index.
To make tables with alternating row colors and such, use
var?item_parity
orvar?item_cycle(...)
instead.
is_first
Tells if the item where the iteration (which is identified by the loop variable name) currently stands is the first item.
is_last
Tells if the item where the iteration (which is identified by the loop variable name) currently stands is the last item.
If you need the inverse of this built-in, use
var?has_next
instead of!var?is_last
, because it’s more readable.
For separating items with commas and such, use
<#sep>separator</#sep>
instead of<#if var?has_next>separator</#if>
, as it’s more readable. (Furthermore the</#sep>
can be often omitted, like in<#list ... as var>...${var}...<#sep>separator</#list>
)
is_odd_item
Tells if the item where the iteration (which is identified by the loop variable name) currently stands has an odd 1-based index.
To make tables with alternating row colors and such, use
var?item_parity
orvar?item_cycle(...)
instead.
item_cycle
This is a more generic version of the item_parity built-in, where you can specify what value to use instead of “odd” and “even”. It also allows more than 2 values that it will cycle through.
Some details:
-
The number of arguments must be at least 1, and has no upper limit.
-
The type of the arguments can be anything, they doesn’t have to be strings.
Use the
item_parity
built-in instead if the values you need are"odd"
and"even"
.
item_parity
Returns "odd"
or "even"
string value, depending on the parity of the 1-based index where the iteration (which is identified by the loop variable name) currently stands. This is commonly used for alternating color for table rows:
Use the
item_parity_cap
built-in for capitalized"Odd"
and"Even"
. Use theitem_cycle
built-in to specify custom values, or more then two values.
item_parity_cap
Returns "Odd"
or "Even"
string value (note the capitalization), depending on the parity of the 1-based index where the iteration (which is identified by the loop variable name) currently stands.
Use the
item_parity
built-in for lower case"odd"
and"even"
.