{section},{sectionelse} {section} は、 データの配列 をループするために使用します。 これは、{foreach}1つの連想配列 をループするのとは異なります。すべての {section} タグは、終了タグ {/section} とペアになっている必要があります。 属性名 必須 デフォルト 概要 name string Yes n/a セクション名 loop mixed Yes n/a ループ回数を決定する値 start integer No 0 ループを開始するインデックス位置。この値が負の場合は、 配列の最後尾から開始位置が算出されます。 例えばループ配列に7つの値があり、そしてstartが-2であるならば、 開始インデックスは5になります。 ループ配列の長さを超えるような無効な値は、 自動的に最も近い値に切り捨てられます。 step integer No 1 ループインデックスを進めるために使われるステップ値。 例えばstep=2なら、インデックスは0, 2, 4をループします。 stepの値が負の場合は、配列の前方に向かって進みます。 max integer No n/a セクションがループする最大の回数 show boolean No &true; このセクションを表示するかどうか 必須の属性は nameloop です。 {section}name は、 英数字とアンダースコアを使って自由に命名できます。これは PHP の変数 と同様です。 {section} はネスト可能で、その場合の {section} の名前はお互いにユニークである必要があります。 loop 属性で指定されたループ変数 (たいていは配列) は、{section} のループ回数を決定するために使用されます。 loop の値として、整数値を渡すこともできます。 {section} 内で値を表示するには、 変数名に続けてブラケット {} で囲んだセクション名を指定します。 ループ変数に値が存在しない場合は {sectionelse} が実行されます。 {section} には、そのプロパティを操作するための 自身の変数があります。これらには {$smarty.section.name.property} としてアクセスできます。name は、name 属性の値です。 {section} のプロパティには、 indexindex_previndex_nextiterationfirstlastrownumloopshowtotal があります。 {section} でのシンプルな配列のループ 配列を Smarty に assign() します。 assign('custid',$data); ?> ]]> 配列を出力するテンプレート {/section}
{* $custid 配列のすべての値を逆順に表示します *} {section name=foo loop=$custid step=-1} {$custid[foo]}
{/section} ]]>
上の例の出力 id: 1001
id: 1002

id: 1002
id: 1001
id: 1000
]]>
{section} で配列を割り当てない例 {section name=bar loop=21 max=6 step=-2} {$smarty.section.bar.index} {/section} ]]> 上の例の出力 20 18 16 14 12 10 ]]> {section} の名前 {section}name は自由につけることができます。PHP の変数 を参照してください。これは、{section} 内のデータを参照する際に使用します。 {section} での連想配列のループ これは、データの連想配列を {section} で出力する例です。 次に示すのは、配列 $contacts を Smarty に渡す PHP スクリプトです。 'John Smith', 'home' => '555-555-5555', 'cell' => '666-555-5555', 'email' => 'john@myexample.com'), array('name' => 'Jack Jones', 'home' => '777-555-5555', 'cell' => '888-555-5555', 'email' => 'jack@myexample.com'), array('name' => 'Jane Munson', 'home' => '000-555-5555', 'cell' => '123456', 'email' => 'jane@myexample.com') ); $smarty->assign('contacts',$data); ?> ]]> $contacts を出力するテンプレート name: {$contacts[customer].name}
home: {$contacts[customer].home}
cell: {$contacts[customer].cell}
e-mail: {$contacts[customer].email}

{/section} ]]>
上の例の出力 name: John Smith
home: 555-555-5555
cell: 666-555-5555
e-mail: john@myexample.com

name: Jack Jones
home phone: 777-555-5555
cell phone: 888-555-5555
e-mail: jack@myexample.com

name: Jane Munson
home phone: 000-555-5555
cell phone: 123456
e-mail: jane@myexample.com

]]>
{section} での <varname>loop</varname> 変数の使用 この例では、$custid$name および $address にはすべて配列が割り当てられ、 その要素数は同じであるものとします。まず、Smarty に配列を割り当てる PHP スクリプトです。 assign('custid',$id); $fullnames = array('John Smith','Jack Jones','Jane Munson'); $smarty->assign('name',$fullnames); $addr = array('253 Abbey road', '417 Mulberry ln', '5605 apple st'); $smarty->assign('address',$addr); ?> ]]> loop 変数は、ループの回数を決定するためにのみ使用します。 {section} 内ではあらゆるテンプレート変数にアクセス可能です。 id: {$custid[customer]}
name: {$name[customer]}
address: {$address[customer]}

{/section} ]]>
上の例の出力 id: 1000
name: John Smith
address: 253 Abbey road

id: 1001
name: Jack Jones
address: 417 Mulberry ln

id: 1002
name: Jane Munson
address: 5605 apple st

]]>
ネストした {section} {section} は無制限にネスト可能です。{section} をネストすることで、 多次元配列のような複雑なデータ構造にアクセスすることが可能です。 これは、配列を割り当てる .php スクリプトの例です。 assign('custid',$id); $fullnames = array('John Smith','Jack Jones','Jane Munson'); $smarty->assign('name',$fullnames); $addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st'); $smarty->assign('address',$addr); $types = array( array( 'home phone', 'cell phone', 'e-mail'), array( 'home phone', 'web'), array( 'cell phone') ); $smarty->assign('contact_type', $types); $info = array( array('555-555-5555', '666-555-5555', 'john@myexample.com'), array( '123-456-4', 'www.example.com'), array( '0457878') ); $smarty->assign('contact_info', $info); ?> ]]> このテンプレートでは、$contact_type[customer] は現在の顧客の連絡手段を格納した配列となります。 id: {$custid[customer]}
name: {$name[customer]}
address: {$address[customer]}
{section name=contact loop=$contact_type[customer]} {$contact_type[customer][contact]}: {$contact_info[customer][contact]}
{/section} {/section} ]]>
上の例の出力。 id: 1000
name: John Smith
address: 253 N 45th
home phone: 555-555-5555
cell phone: 666-555-5555
e-mail: john@myexample.com

id: 1001
name: Jack Jones
address: 417 Mulberry ln
home phone: 123-456-4
web: www.example.com

id: 1002
name: Jane Munson
address: 5605 apple st
cell phone: 0457878
]]>
データベースを使用する {sectionelse} の例 データベース (ADODB や PEAR) の検索結果を Smarty に格納します。 assign('contacts', $db->getAll($sql)); ?> ]]> データベースの結果を HTML のテーブルに出力するテンプレート  Name>HomeCellEmail {section name=co loop=$contacts} view {$contacts[co].name} {$contacts[co].home} {$contacts[co].cell} {$contacts[co].email} {sectionelse} No items found {/section} ]]> .index index は現在のループインデックスを表示します。 0(又は start 属性の値)から始まり、 1(又は step 属性の値)ずつ増加します。 テクニカルノート stepstart 属性が変更されていない場合は、セクションのプロパティ iteration と同じ動作をします。ただ、1 ではなく 0 から始まるという点が異なります。 {section} の <varname>index</varname> プロパティ ちなみに…… $custid[customer.index]$custid[customer] は同じ意味です。 {/section} ]]> 上の例の出力 1 id: 1001
2 id: 1002
]]>
.index_prev index_prev は前回のループインデックスを表示します。最初のループでは-1がセットされます。 .index_next index_next は次回のループインデックスを表示します。 ループの最後でもやはり現在のインデックスの次回の値を返します (step 属性の設定に従います)。 <varname>index</varname>、<varname>index_next</varname> および <varname>index_prev</varname> プロパティ assign('rows',$data); ?> ]]> 上の配列をテーブルに出力するテンプレート indexid index_prevprev_id index_nextnext_id {section name=row loop=$rows} {$smarty.section.row.index}{$rows[row]} {$smarty.section.row.index_prev}{$rows[row.index_prev]} {$smarty.section.row.index_next}{$rows[row.index_next]} {/section} ]]> 上の例の出力するテーブルは次のようになります。 .iteration iteration は現在のループが反復された回数を表示します。 index プロパティとは異なり、これは {section} のプロパティ startstep および max の影響を受けません。 iteration も 1 から始まります。これは index が 0 から始まるのとは異なります。rownumiteration の別名で、全く同じ働きをします。 セクションのプロパティ <varname>iteration</varname> assign('arr',$id); ?> ]]> $arr 配列の要素を step=2 で出力するテンプレート {/section} ]]> 上の例の出力 iteration=2 index=7 id=3007
iteration=3 index=9 id=3009
iteration=4 index=11 id=3011
iteration=5 index=13 id=3013
iteration=6 index=15 id=3015
]]>
もうひとつの例は、iteration プロパティを使用して 5 行おきにテーブルのヘッダ部を出力します。 {if} 関数を mod 演算子とともに使用します。 {section name=co loop=$contacts} {if $smarty.section.co.iteration % 5 == 1}  Name>HomeCellEmail {/if}
view {$contacts[co].name} {$contacts[co].home} {$contacts[co].cell} {$contacts[co].email} {/section} ]]> .first first は、現在 {section} の一回目の処理を行っている場合に &true; となります。 .last last は、現在 {section} の最後の処理を行っている場合に &true; となります。 {section} プロパティ <varname>first</varname> と <varname>last</varname> この例は $customers 配列をループし、 ループの最初でヘッダブロック、そしてループの最後でフッタブロックを出力します。 total プロパティも使用します。 idcustomer {/if} {$customers[customer].id}} {$customers[customer].name} {if $smarty.section.customer.last} {$smarty.section.customer.total} customers {/if} {/section} ]]> .rownum rownum は現在のループが反復された回数を表示します(1から開始)。 これは iteration の別名で、同じ動作をします。 .loop loop は、この {section} ループの最後のインデックス番号を表示します。 {section} の内部だけでなく、外部で使用することもできます。 {section} プロパティ <varname>loop</varname> {/section} There are {$smarty.section.customer.loop} customers shown above. ]]> 上の例の出力 1 id: 1001
2 id: 1002
There are 3 customers shown above. ]]>
.show show は、セクションのパラメータとして使用する boolean 値です。&false; の場合はこのセクションは表示されません。 {sectionelse} があれば、それが代わりに表示されます。 <varname>show</varname> プロパティ Boolean $show_customer_info を PHP アプリケーションから渡し、このセクションを表示するかどうかを調整します。 {/section} {if $smarty.section.customer.show} the section was shown. {else} the section was not shown. {/if} ]]> 上の例の出力 2 id: 1001
3 id: 1002
the section was shown. ]]>
.total total{section} がループしたトータル回数を表示します。これは {section} の内部だけでなく外部でも使うことができます。 <varname>total</varname> プロパティの例 {/section} There are {$smarty.section.customer.total} customers shown above. ]]> {foreach} および $smarty.section も参照してください。