Sponsored Link

foreach

foreach tag behaves like the foreach statement of PHP.
Nest of foreach tag is also possible.
const::FOREACH_INDEX (default:@index) is the key of the foreach.
const::FOREACH_KEY (default:@key) is the key of the foreach.
const::FOREACH_VAL (default:@val) is the value of the foreach.

Syntax

{{ @foreach val_name }}
    statements
{{ @endforeach }}

PHP

$assign['sample_foreach1'] = array('id'=>1, 'name'=>'RHEL', 'vendor'=>'RedHat');

$assign['sample_foreach2']['a2'] = array('id'=>2, 'name'=>'MacOS', 'vendor'=>'Apple');

$assign['sample_foreach3'][] = array('id'=>31, 'name'=>array('os'=>'Linux', 'dist'=>'debian'));
$assign['sample_foreach3'][] = array('id'=>32, 'name'=>array('os'=>'Linux', 'dist'=>'Ubuntu'));

$assign['sample_foreach4']['a4'][] = array('id'=>41, 'name'=>'RedHat & CentOS');
$assign['sample_foreach4']['a4'][] = array('id'=>42, 'name'=>'MacOS & iOS');

Template

{{ @foreach sample_foreach1 }}
    {{ @key }} => {{ @val }}<br>
{{ @endforeach }}
<br>
{{ @foreach sample_foreach2.a2 }}
    {{ @key }} => {{ @val }}<br>
{{ @endforeach }}
<br>
{{ @foreach sample_foreach3 }}
    {{ @key }} => {{ .id }} => {{ .name.os }} => {{ .name.dist }}<br>
{{ @endforeach }}
<br>
{{ @foreach sample_foreach4.a4 }}
    loop-index = {{ @index }}<br>
    {{ @key }} => {{ .id }} => {{ .name|nbsp }}<br>
{{ @endforeach }}

Result

id => 1
name => RHEL
vendor => RedHat

id => 2
name => MacOS
vendor => Apple

0 => 31 => Linux => debian
1 => 32 => Linux => Ubuntu

loop-index = 1
0 => 41 => RedHat & CentOS
loop-index = 2
1 => 42 => MacOS & iOS