Sponsored Link

foreach + var

var タグは、foreach タグ内でも利用可能です。

PHP

$assign['sample_foreach8']['a8'][] = array('id'=>81, 'name'=>'Firefox');
$assign['sample_foreach8']['a8'][] = array('id'=>82, 'name'=>'Safari');
$assign['sample_foreach8']['a8'][] = array('id'=>83, 'name'=>'dummy');
$assign['sample_foreach9'] = array('FreeBSD', 'NETBSD', 'openbsd');

Template

{{ @var tpl_var_if = 'Firefox' }}
{{ @var tpl_var_elseif.a8 = 'Safari' }}
{{ @foreach sample_foreach8.a8 }}
    {{ @if .name == tpl_var_if }}
        if -- id={{ .id }}, name={{ .name }}<br>
    {{ @elseif .name == tpl_var_elseif.a8 }}
        elseif -- id={{ .id }}, name={{ .name }}<br>
    {{ @else }}
        else -- id={{ .id }}, name={{ .name }}<br>
    {{ @endif }}
{{ @endforeach }}
<br>
{{ @foreach sample_foreach9 }}
    {{ @if @key === 1 }}
        {{ @var @val = 'NetBSD' }}
    {{ @endif }}
    {{ @if @val === 'openbsd' }}
        {{ @var @val = 'OpenBSD' }}
    {{ @endif }}
    {{ @val }}<br>
{{ @endforeach }}

Result

if -- id=81, name=Firefox
elseif -- id=82, name=Safari
else -- id=83, name=dummy

FreeBSD
NetBSD
OpenBSD