Loop

{loop} — Loops over an array of arrays and allows for really simple / small constructs by moving the scope down to each element automatically. It is basically a combination of foreach and with internally.
{loopelse} or {else} is executed when there are no values in the array variable.

Note that this plugin supports iterator variables through the name parameter and also supports the else plugin
Examples:

<?php
$tplix->assign('contacts', [
         ['name' => 'khawar', 'cell' => '555-555-0357'],
         ['name' => 'hassan','cell' => '800-555-2222'],
     ]);

Where template is:

Example 1:

{loop $contacts}
    {$name|capitalize}: ({$cell})<br>
{else}
Data not found
{/loop}

The above example will output:


Khawar: (+921231231231)
Waleed: (+921241241241)
Naveed: (+921251251251)
Ch Amir: (+921261261261)
Example 2: {$_key} will return loop uniue key

{loop $contacts as $a}
    {$_key}-{$name|capitalize}: ({$cell})<br>
{loopelse}
Data not found
{/loop}

The above example will output:


0-Khawar: (+921231231231)
1-Waleed: (+921241241241)
2-Naveed: (+921251251251)
3-Ch Amir: (+921261261261)
Example 3:

{loop $contacts as $k name="myloop"}
    {$name|capitalize}: ({$cell})<br>
{/loop}

The above example will output:


Khawar: (+921231231231)
Waleed: (+921241241241)
Naveed: (+921251251251)
Ch Amir: (+921261261261)
Example 4:

{loop $contacts as $k=>$c name="myloop"}
    {$name|capitalize}: ({$cell})<br>
{/loop}

The above example will output:


Khawar: (+921231231231)
Waleed: (+921241241241)
Naveed: (+921251251251)
Ch Amir: (+921261261261)
Accessing the array key

To access the array key you have to use the {$_key} var within the loop, its name is not user-definable to keep this plugin as simple as possible.

Was this article helpful?