For
{for}The {for}{forelse} tag is used to create simple loops. The following different formats are supported:
- {for $var=$start to $end} simple loop with step size of 1.
- {for $var=$start to $end step $step} loop with individual step size.
- {forelse} or {else} is executed when the loop is not iterated.
- name: foreach name to access it’s iterator variables
- from: array to iterate from (which will translate to 0) or a number as a start value
- to: value to stop iterating at (if you set an array in
$from, this is automatically set tocount($array)) - step: defines the incrementation of the pointer at each iteration
Examples:Note that this plugin supports iterator variables through the name parameter and also supports the else plugin
Example:1 Simple Loop 0 to 5
<ul>
{for foo 0 5}
<li>{$foo}</li>
{/for}
</ul>
Output
<ul>
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
Example:2 Loop 0 to 5 with steps, in this example 0 as start, 5 as end and 2 as steps
<ul>
{for i 0 5 2} <li>{$i}</li> {/for}
</ul>
Output
<ul>
<li>0</li>
<li>2</li>
<li>4</li>
</ul>
Example:3 Loop 1 to 5 and $foo as loop increment, in this example 1 as start, 5 as end
<ul>
{for $foo=1 to 5}
<li>{$foo}</li>
{else}
Data not found
{/for}
</ul>
Output
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
Example:4 Loop 3 to 10 and $foo as loop increment, in this example 3 as start, $to as end from PHP assigned value
<ul>
{for $foo=3 to $to}
<li>{$foo}</li>
{forelse}
Data not found
{/for}
</ul>
Output
<ul>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
Example:5 Loop $start to $to and $foo as loop increment, in this example $start and $to as assigned value from PHP
<ul>
{for $foo=$start to=$to}
<li>{$foo}</li>
{/for}
</ul>
Output
<ul>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>