section,sectionelse
sectionは、データの配列をループするために使用されます。全てのsectionと/sectionはペアである必要があります。必須のパラメータは、nameとloopです。セクション名は英数字とアンダースコアを使って自由に命名できます。このタグはネスト可能で、その場合のセクション名はお互いにユニークである必要があります。loop属性で指定されたループ変数(たいていは配列)は、セクションのループ回数を決定するためにのみ使用されます。
セクション内で値を表示するには、変数名の次にブランケット[]で囲んだセクション名を指定します。ループ変数に値が存在しない場合は、sectionelseが実行されます。
例 7-15. section {* $custid配列の値を全て表示 *}
{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{/section}
出力:
id: 1000<br>
id: 1001<br>
id: 1002<br> |
|
例 7-16. section ループ変数 {* ループ変数は、ループ回数を決定するためにのみ使用されます。
セクション内ではあらゆるテンプレート変数にアクセス可能です。
この例では、$custid, $name, $addressのそれぞれの配列には同じ個数の値が格納されているとします。 *}
{section name=customer loop=$custid}
id: {$custid[customer]}<br>
name: {$name[customer]}<br>
address: {$address[customer]}<br>
<p>
{/section}
出力:
id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
<p> |
|
例 7-17. セクション名 {* セクション名は自由に命名可能で、セクション内で配列を参照するために使われます。 *}
{section name=mydata loop=$custid}
id: {$custid[mydata]}<br>
name: {$name[mydata]}<br>
address: {$address[mydata]}<br>
<p>
{/section} |
|
例 7-18. ネストしたセクション {* セクションは無制限にネスト可能です。ネストしたセクションによって、
多次元配列のような複雑なデータ構造にアクセスする事が可能です。この例では、
$contact_type[customer]は現在の顧客の連絡方法を格納した配列を示します。 *}
{section name=customer loop=$custid}
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}
<p>
{/section}
出力:
id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: john@mydomain.com<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<br>
<p> |
|
例 7-19. セクションと連想配列 {* セクション内に連想配列の値を表示 *}
{section name=customer loop=$contacts}
name: {$contacts[customer].name}<br>
home: {$contacts[customer].home}<br>
cell: {$contacts[customer].cell}<br>
e-mail: {$contacts[customer].email}<p>
{/section}
出力:
name: John Smith<br>
home: 555-555-5555<br>
cell: 555-555-5555<br>
e-mail: john@mydomain.com<p>
name: Jack Jones<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<p>
name: Jane Munson<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<p> |
|
例 7-20. sectionelse {* sectionelseは、$custidに値が存在しない場合に実行されます *}
{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{sectionelse}
there are no values in $custid.
{/section} |
|
セクションには、セクションプロパティを操作するための自身の変数があります。
これらは、{$smarty.section.sectionname.varname}のように示されます。
注意:
(注: Smarty 1.5.0.から、セクションプロパティ変数の書式が{%sectionname.varname%}から{$smarty.section.sectionname.varname}に変更されました。古い書式はまだサポートされていますが、マニュアルの例では新しい書式を使用しています)
index
現在のループインデックスを表示します。0(又はstart属性の値)から始まり、1(又はstep属性の値)ずつ増加します。
テクニカルノート:
step及びstart属性が変更されていない場合は、セクションプロパティのiterationと同じ動作をします。(1の代わりに0から始まる)
例 7-21. セクションプロパティ index {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_prev
前回のループインデックスを表示します。最初のループでは-1がセットされます。
例 7-22. セクションプロパティ index_prev {section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{* 参考までに言うと、$custid[customer.index] と $custid[customer] は同じ意味です。 *}
{if $custid[customer.index_prev] ne $custid[customer.index]}
The customer id changed<br>
{/if}
{/section}
出力:
0 id: 1000<br>
The customer id changed<br>
1 id: 1001<br>
The customer id changed<br>
2 id: 1002<br>
The customer id changed<br> |
|
index_next
次回のループインデックスを表示します。ループの最後でもやはり現在のインデックスの次回の値を返します。(step 属性の設定に従います)
例 7-23. セクションプロパティ index_next {section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{* 参考までに言うと、$custid[customer.index] と $custid[customer] は同じ意味です。 *}
{if $custid[customer.index_next] ne $custid[customer.index]}
The customer id will change<br>
{/if}
{/section}
出力:
0 id: 1000<br>
The customer id will change<br>
1 id: 1001<br>
The customer id will change<br>
2 id: 1002<br>
The customer id will change<br> |
|
iteration
現在のループが反復された回数を表示します。
注意:
(注: indexセクションプロパティとは異なり、start, step, max属性に影響されません。
また、0の代わりに1から始まります。rownumはiterationの別名で、全く同じ働きをします。)
例 7-24. セクションプロパティ iteration {section name=customer loop=$custid start=5 step=2}
current loop iteration: {$smarty.section.customer.iteration}<br>
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{* 参考までに言うと、$custid[customer.index] と $custid[customer] は同じ意味です。 *}
{if $custid[customer.index_next] ne $custid[customer.index]}
The customer id will change<br>
{/if}
{/section}
出力:
current loop iteration: 1
5 id: 1000<br>
The customer id will change<br>
current loop iteration: 2
7 id: 1001<br>
The customer id will change<br>
current loop iteration: 3
9 id: 1002<br>
The customer id will change<br> |
|
first
現在のセクションの反復が一回目の場合にtrueがセットされます。
例 7-25. セクションプロパティ first {section name=customer loop=$custid}
{if $smarty.section.customer.first}
<table>
{/if}
<tr><td>{$smarty.section.customer.index} id:
{$custid[customer]}</td></tr>
{if $smarty.section.customer.last}
</table>
{/if}
{/section}
出力:
<table>
<tr><td>0 id: 1000</td></tr>
<tr><td>1 id: 1001</td></tr>
<tr><td>2 id: 1002</td></tr>
</table> |
|
last
現在のセクションの反復が最後の場合にtrueがセットされます。
例 7-26. セクションプロパティ last {section name=customer loop=$custid}
{if $smarty.section.customer.first}
<table>
{/if}
<tr><td>{$smarty.section.customer.index} id:
{$custid[customer]}</td></tr>
{if $smarty.section.customer.last}
</table>
{/if}
{/section}
出力:
<table>
<tr><td>0 id: 1000</td></tr>
<tr><td>1 id: 1001</td></tr>
<tr><td>2 id: 1002</td></tr>
</table> |
|
rownum
現在のループが反復された回数を表示します(1から開始)。rownumはiterationの別名で、それらは同じ働きをします。
例 7-27. セクションプロパティ rownum {section name=customer loop=$custid}
{$smarty.section.customer.rownum} id: {$custid[customer]}<br>
{/section}
出力:
1 id: 1000<br>
2 id: 1001<br>
3 id: 1002<br> |
|
loop
セクションがループした最後のインデックスを表示します。これはセクションの外でも使う事ができます。
例 7-28. セクションプロパティ index {section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{/section}
There were {$smarty.section.customer.loop} customers shown above.
出力:
0 id: 1000<br>
1 id: 1001<br>
2 id: 1002<br>
There were 3 customers shown above. |
|
show
セクションの表示/非表示を決定します。show属性は、true/falseのboolean値です。これがfalseの場合はセクションは表示されません。{sectionelse}があれば、それが代わりに表示されます。
例 7-29. セクションの属性 show {* $show_customer_infoは、このセクションの表示/非表示を決めるために
phpアプリケーションから渡された変数です *}
{section name=customer loop=$custid show=$show_customer_info}
{$smarty.section.customer.rownum} id: {$custid[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
セクションがループしたトータル回数を表示します。これはセクションの外でも使う事ができます。
例 7-30. セクションプロパティ total {section name=customer loop=$custid step=2}
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{/section}
There were {$smarty.section.customer.total} customers shown above.
出力:
0 id: 1000<br>
2 id: 1001<br>
4 id: 1002<br>
There were 3 customers shown above. |
|