Skip to main content

for

for loop

for loops can be applied by defining a counter variable and a number range. Alternatively, an iteration can be performed through an array.

Video tutorial

Number range

For this variation, a counter variable (in this case$_i) and then a number range (in this case from 1 to 10) need to be defined:

{% for $_i in 1..10 %}
	Hello, I am pass $_i
{% endfor %}

Alternative

{%
	for($_i in 1..10)

	{
		print("Hello, I am pass " .$_i);
	}

	# Output:
	# Hello, I am pass 1
	# Hello, I am pass 2
	# ...
	# Hello, I am pass 10
%}

This variation is optional and can e.g. be used to display item images. Item images are made available within the templates as arrays.

If you have saved more than one but no more than five images for some items, the following loop is an interesting option for displaying images:

{% for $_i in 1..5 %}
	{% if $ImageURL[$_i]!="" %}
		$Image[$_i]
	{% endif %}
{% endfor %}

Alternative

{%
	for($_i in 1..5)
	{
		if($ImageURL[$_i] != "")
		{
			print($Image[$_i]);
		}
	}
%}

Iteration through array

An iteration through an array can be performed in two different ways. Either the value of the current array position is returned or both the position and the value.

{% $_a = ["Hello", "there!", "How", "are", "you", "today?"] %}

{% for $_word in $_a %}
	$_word
{% endfor %}

Alternative

{%
	$_a = ["Hello", "there!", "How", "are", "you", "today?"];

		for($_word in $_a)
		{
			print($_word);
		}

	# Output: Hello there! How are you today?
%}

Use the following loop variation if you want to return the position (in this case$_key) as well:

Iteration through array with loop for single-line code block

{% $_a = ["Hello", "there!", "How", "are", "you", "today?"] %}

{% for $_key, $_word in $_a %}
	Word $_key: $_word
{% endfor %}

Alternative

{%
	for($_key, $_word in $_a)

	{
		print("Word ");
		print($_key);
		print(": ");
		print($_word);
	}

	# Output:
	# Word 1: Hello Word 2: there! ...
%}

Available template variables

The following template variables are available within for loops. If several loops are nested, the numbering is done from outside to inside.

$LoopBreak[1]

$LoopContinue[1]

$LoopIsFirst[1]

$LoopIsLast[1]

$LoopCount[1]

$LoopPosition[1]

$LoopRevPosition[1]
To top