For Loops
As of March 2017, Branch Code is deprecated and no longer developed. Use the Twig syntax instead.
For loops let you repeatedly execute some code while a condition is true.
Below is a basic example.
{for num="3"}
Code to execute here
{/for}
Parameters
There are three parameters for the 'for' tag that control how many times to repeat the loop, what number to start at and how much to increment the number in each loop.
Parameter | Description |
---|---|
num |
Description: The number of times to repeat the loop. Type: Integer Required: Yes Example: num="3" |
startAt |
Description: The number to start at in the loop. Type: Integer Required: No Default Value: 1 Example: startAt="5" |
increment |
Description: How much to increment the number in each loop. Type: Integer Required: No Default Value: 1 Example: increment="2" |
Counter Variable
Within the for loop a single variable gets created to keep track of what number you are at. You can use this variable within the loop.
{#i}
That variable will always hold an integer value. You should refrain from trying to change it's value as it could mess with the logic within the for loop.
Examples
Example 1
The following example will execute the code 5 time. The counter variable ("i") will start at 1 and increment by 1 until it reaches a value of 5.
{for num="5"}
<p>Counter value: {#i}</p>
{/for}
The result of the above code would be:
<p>Counter value: 1</p>
<p>Counter value: 2</p>
<p>Counter value: 3</p>
<p>Counter value: 4</p>
<p>Counter value: 5</p>
Example 2
The following example will execute the code 3 times. The counter variable will start at 10 and increment by 1 until it reaches a value of 12.
{for num="3" startAt="10"}
<p>I am {#i} years old</p>
{/for}
The result of the above code would be:
<p>I am 10 years old</p>
<p>I am 11 years old</p>
<p>I am 12 years old</p>
Example 3
The following example will execute the code 5 times. The counter variable will start at 6 and increment by 2 until it reaches a value of 14.
<table>
<tr>
{for num="5" startAt="6" increment="2"}
<td>{#i}</td>
{/for}
</tr>
</table>
The result from the above code would be:
<table>
<tr>
<td>6</td>
<td>8</td>
<td>10</td>
<td>12</td>
<td>14</td>
</tr>
</table>