What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
{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 |
このセクションを表示するかどうか |
必須の属性は name
と loop
です。
{section}
の name
は、
英数字とアンダースコアを使って自由に命名できます。これは
PHP の変数
と同様です。
{section} はネスト可能で、その場合の
{section}
の名前はお互いにユニークである必要があります。
loop
属性で指定されたループ変数
(たいていは配列) は、{section}
のループ回数を決定するために使用されます。
loop の値として、整数値を渡すこともできます。
{section}
内で値を表示するには、
変数名に続けてブラケット {} で囲んだセクション名を指定します。
ループ変数に値が存在しない場合は
{sectionelse}
が実行されます。
{section}
には、そのプロパティを操作するための
自身の変数があります。これらには
{$smarty.section.name.property}
としてアクセスできます。“name” は、name
属性の値です。
{section}
のプロパティには、
index
、
index_prev
、
index_next
、
iteration
、
first
、
last
、
rownum
、
loop
、
show
、
total
があります。
Example 7.30. {section} でのシンプルな配列のループ
配列を Smarty に assign()
します。
<?php $data = array(1000,1001,1002); $smarty->assign('custid',$data); ?>
配列を出力するテンプレート
{* この例は $custid 配列のすべての値を表示します *} {section name=customer loop=$custid} id: {$custid[customer]}<br /> {/section} <hr /> {* $custid 配列のすべての値を逆順に表示します *} {section name=foo loop=$custid step=-1} {$custid[foo]}<br /> {/section}
上の例の出力
id: 1000<br /> id: 1001<br /> id: 1002<br /> <hr /> id: 1002<br /> id: 1001<br /> id: 1000<br />
Example 7.31. {section} で配列を割り当てない例
{section name=foo start=10 loop=20 step=2} {$smarty.section.foo.index} {/section} <hr /> {section name=bar loop=21 max=6 step=-2} {$smarty.section.bar.index} {/section}
上の例の出力
10 12 14 16 18 <hr /> 20 18 16 14 12 10
Example 7.32. {section} の名前
{section}
の name
は自由につけることができます。PHP
の変数 を参照してください。これは、{section}
内のデータを参照する際に使用します。
{section name=anything loop=$myArray} {$myArray[anything].foo} {$name[anything]} {$address[anything].bar} {/section}
Example 7.33. {section} での連想配列のループ
これは、データの連想配列を
{section}
で出力する例です。
次に示すのは、配列 $contacts
を Smarty に渡す PHP スクリプトです。
<?php $data = array( array('name' => '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
を出力するテンプレート
{section name=customer loop=$contacts} <p> name: {$contacts[customer].name}<br /> home: {$contacts[customer].home}<br /> cell: {$contacts[customer].cell}<br /> e-mail: {$contacts[customer].email} </p> {/section}
上の例の出力
<p> name: John Smith<br /> home: 555-555-5555<br /> cell: 666-555-5555<br /> e-mail: john@myexample.com </p> <p> name: Jack Jones<br /> home phone: 777-555-5555<br /> cell phone: 888-555-5555<br /> e-mail: jack@myexample.com </p> <p> name: Jane Munson<br /> home phone: 000-555-5555<br /> cell phone: 123456<br /> e-mail: jane@myexample.com </p>
Example 7.34. {section} での loop
変数の使用
この例では、$custid
、$name
および $address
にはすべて配列が割り当てられ、
その要素数は同じであるものとします。まず、Smarty に配列を割り当てる
PHP スクリプトです。
<?php $id = array(1001,1002,1003); $smarty->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}
内ではあらゆるテンプレート変数にアクセス可能です。
{section name=customer loop=$custid} <p> id: {$custid[customer]}<br /> name: {$name[customer]}<br /> address: {$address[customer]} </p> {/section}
上の例の出力
<p> id: 1000<br /> name: John Smith<br /> address: 253 Abbey road </p> <p> id: 1001<br /> name: Jack Jones<br /> address: 417 Mulberry ln </p> <p> id: 1002<br /> name: Jane Munson<br /> address: 5605 apple st </p>
Example 7.35. ネストした {section}
{section} は無制限にネスト可能です。{section} をネストすることで、
多次元配列のような複雑なデータ構造にアクセスすることが可能です。
これは、配列を割り当てる .php
スクリプトの例です。
<?php $id = array(1001,1002,1003); $smarty->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] は現在の顧客の連絡手段を格納した配列となります。
{section name=customer loop=$custid} <hr> id: {$custid[customer]}<br /> name: {$name[customer]}<br /> address: {$address[customer]}<br /> {section name=contact loop=$contact_type[customer]} {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br /> {/section} {/section}
上の例の出力。
<hr> id: 1000<br /> name: John Smith<br /> address: 253 N 45th<br /> home phone: 555-555-5555<br /> cell phone: 666-555-5555<br /> e-mail: john@myexample.com<br /> <hr> id: 1001<br /> name: Jack Jones<br /> address: 417 Mulberry ln<br /> home phone: 123-456-4<br /> web: www.example.com<br /> <hr> id: 1002<br /> name: Jane Munson<br /> address: 5605 apple st<br /> cell phone: 0457878<br />
Example 7.36. データベースを使用する {sectionelse} の例
データベース (ADODB や PEAR) の検索結果を Smarty に格納します。
<?php $sql = 'select id, name, home, cell, email from contacts ' ."where name like '$foo%' "; $smarty->assign('contacts', $db->getAll($sql)); ?>
データベースの結果を HTML のテーブルに出力するテンプレート
<table> <tr><th> </th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr> {section name=co loop=$contacts} <tr> <td><a href="view.php?id={$contacts[co].id}">view<a></td> <td>{$contacts[co].name}</td> <td>{$contacts[co].home}</td> <td>{$contacts[co].cell}</td> <td>{$contacts[co].email}</td> <tr> {sectionelse} <tr><td colspan="5">No items found</td></tr> {/section} </table>
index
は現在のループインデックスを表示します。
0(又は start
属性の値)から始まり、
1(又は step
属性の値)ずつ増加します。
step
と start
属性が変更されていない場合は、セクションのプロパティ iteration
と同じ動作をします。ただ、1 ではなく 0 から始まるという点が異なります。
Example 7.37. {section} の index
プロパティ
$custid[customer.index]
と
$custid[customer]
は同じ意味です。
{section name=customer loop=$custid} {$smarty.section.customer.index} id: {$custid[customer]}<br /> {/section}
上の例の出力
0 id: 1000<br /> 1 id: 1001<br /> 2 id: 1002<br />
index_next
は次回のループインデックスを表示します。
ループの最後でもやはり現在のインデックスの次回の値を返します
(step
属性の設定に従います)。
Example 7.38. index
、index_next
および index_prev
プロパティ
<?php $data = array(1001,1002,1003,1004,1005); $smarty->assign('rows',$data); ?>
上の配列をテーブルに出力するテンプレート
{* $rows[row.index] と $rows[row] は同じ意味です *} <table> <tr> <th>index</th><th>id</th> <th>index_prev</th><th>prev_id</th> <th>index_next</th><th>next_id</th> </tr> {section name=row loop=$rows} <tr> <td>{$smarty.section.row.index}</td><td>{$rows[row]}</td> <td>{$smarty.section.row.index_prev}</td><td>{$rows[row.index_prev]}</td> <td>{$smarty.section.row.index_next}</td><td>{$rows[row.index_next]}</td> </tr> {/section} </table>
上の例の出力するテーブルは次のようになります。
index id index_prev prev_id index_next next_id 0 1001 -1 1 1002 1 1002 0 1001 2 1003 2 1003 1 1002 3 1004 3 1004 2 1003 4 1005 4 1005 3 1004 5
iteration
は現在のループが反復された回数を表示します。
index
プロパティとは異なり、これは {section}
のプロパティ
start
、step
および max
の影響を受けません。
iteration
も 1 から始まります。これは
index
が 0 から始まるのとは異なります。rownum
は iteration
の別名で、全く同じ働きをします。
Example 7.39. セクションのプロパティ iteration
<?php // 3000 から 3015 までの配列 $id = range(3000,3015); $smarty->assign('arr',$id); ?>
$arr
配列の要素を
step=2
で出力するテンプレート
{section name=cu loop=$arr start=5 step=2} iteration={$smarty.section.cu.iteration} index={$smarty.section.cu.index} id={$custid[cu]}<br /> {/section}
上の例の出力
iteration=1 index=5 id=3005<br /> iteration=2 index=7 id=3007<br /> iteration=3 index=9 id=3009<br /> iteration=4 index=11 id=3011<br /> iteration=5 index=13 id=3013<br /> iteration=6 index=15 id=3015<br />
もうひとつの例は、iteration
プロパティを使用して
5 行おきにテーブルのヘッダ部を出力します。
{if}
関数を mod 演算子とともに使用します。
<table> {section name=co loop=$contacts} {if $smarty.section.co.iteration % 5 == 1} <tr><th> </th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr> {/if} <tr> <td><a href="view.php?id={$contacts[co].id}">view<a></td> <td>{$contacts[co].name}</td> <td>{$contacts[co].home}</td> <td>{$contacts[co].cell}</td> <td>{$contacts[co].email}</td> <tr> {/section} </table>
last
は、現在
{section}
の最後の処理を行っている場合に
TRUE
となります。
Example 7.40. {section} プロパティ first
と last
この例は $customers
配列をループし、
ループの最初でヘッダブロック、そしてループの最後でフッタブロックを出力します。
total
プロパティも使用します。
{section name=customer loop=$customers} {if $smarty.section.customer.first} <table> <tr><th>id</th><th>customer</th></tr> {/if} <tr> <td>{$customers[customer].id}}</td> <td>{$customers[customer].name}</td> </tr> {if $smarty.section.customer.last} <tr><td></td><td>{$smarty.section.customer.total} customers</td></tr> </table> {/if} {/section}
rownum
は現在のループが反復された回数を表示します(1から開始)。
これは iteration
の別名で、同じ動作をします。
loop
は、この
{section} ループの最後のインデックス番号を表示します。
{section}
の内部だけでなく、外部で使用することもできます。
Example 7.41. {section} プロパティ loop
{section name=customer loop=$custid} {$smarty.section.customer.index} id: {$custid[customer]}<br /> {/section} There are {$smarty.section.customer.loop} customers shown above.
上の例の出力
0 id: 1000<br /> 1 id: 1001<br /> 2 id: 1002<br /> There are 3 customers shown above.
show
は、セクションのパラメータとして使用する
boolean 値です。FALSE
の場合はこのセクションは表示されません。
{sectionelse}
があれば、それが代わりに表示されます。
Example 7.42. show
プロパティ
Boolean $show_customer_info
を PHP
アプリケーションから渡し、このセクションを表示するかどうかを調整します。
{section name=customer loop=$customers show=$show_customer_info} {$smarty.section.customer.rownum} id: {$customers[customer]}<br /> {/section} {if $smarty.section.customer.show} the section was shown. {else} the section was not shown. {/if}
上の例の出力
1 id: 1000<br /> 2 id: 1001<br /> 3 id: 1002<br /> the section was shown.
total
は {section}
がループしたトータル回数を表示します。これは
{section}
の内部だけでなく外部でも使うことができます。
Example 7.43. total
プロパティの例
{section name=customer loop=$custid step=2} {$smarty.section.customer.index} id: {$custid[customer]}<br /> {/section} There are {$smarty.section.customer.total} customers shown above.
{foreach}
および
$smarty.section
も参照してください。