Sponsored Link

foreach

foreachタグは、PHPのforeach文のような動作をします。
foreachタグのネストも可能です。
const::FOREACH_INDEX (デフォルト:@index) は、foreach のインデックスです。
const::FOREACH_KEY (デフォルト:@key) は、foreach のキーです。
const::FOREACH_VAL (デフォルト:@val) は、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