mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-09 00:31:45 +01:00
838 lines
24 KiB
XML
838 lines
24 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 2698 Maintainer: takagi Status: ready -->
|
|
<!-- CREDITS: mat-sh,daichi,joe -->
|
|
<sect1 id="language.function.section">
|
|
<title>{section},{sectionelse}</title>
|
|
<para>
|
|
<varname>{section}</varname> は、
|
|
<emphasis role="bold">データの配列</emphasis> をループするために使用します。
|
|
これは、<link linkend="language.function.foreach"><varname>{foreach}</varname></link>
|
|
が <emphasis role="bold">1つの連想配列</emphasis>
|
|
をループするのとは異なります。すべての <varname>{section}</varname>
|
|
タグは、終了タグ <varname>{/section}</varname> とペアになっている必要があります。
|
|
</para>
|
|
|
|
<informaltable frame="all">
|
|
<tgroup cols="5">
|
|
<colspec colname="param" align="center" />
|
|
<colspec colname="type" align="center" />
|
|
<colspec colname="required" align="center" />
|
|
<colspec colname="default" align="center" />
|
|
<colspec colname="desc" />
|
|
<thead>
|
|
<row>
|
|
<entry>属性名</entry>
|
|
<entry>型</entry>
|
|
<entry>必須</entry>
|
|
<entry>デフォルト</entry>
|
|
<entry>概要</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>name</entry>
|
|
<entry>string</entry>
|
|
<entry>Yes</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>セクション名</entry>
|
|
</row>
|
|
<row>
|
|
<entry>loop</entry>
|
|
<entry>mixed</entry>
|
|
<entry>Yes</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>ループ回数を決定する値</entry>
|
|
</row>
|
|
<row>
|
|
<entry>start</entry>
|
|
<entry>integer</entry>
|
|
<entry>No</entry>
|
|
<entry><emphasis>0</emphasis></entry> <entry>
|
|
ループを開始するインデックス位置。この値が負の場合は、
|
|
配列の最後尾から開始位置が算出されます。
|
|
例えばループ配列に7つの値があり、そしてstartが-2であるならば、
|
|
開始インデックスは5になります。
|
|
ループ配列の長さを超えるような無効な値は、
|
|
自動的に最も近い値に切り捨てられます。</entry>
|
|
</row>
|
|
<row>
|
|
<entry>step</entry>
|
|
<entry>integer</entry>
|
|
<entry>No</entry>
|
|
<entry><emphasis>1</emphasis></entry>
|
|
<entry>
|
|
ループインデックスを進めるために使われるステップ値。
|
|
例えばstep=2なら、インデックスは0, 2, 4をループします。
|
|
stepの値が負の場合は、配列の前方に向かって進みます。
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>max</entry>
|
|
<entry>integer</entry>
|
|
<entry>No</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>セクションがループする最大の回数</entry>
|
|
</row>
|
|
<row>
|
|
<entry>show</entry>
|
|
<entry>boolean</entry>
|
|
<entry>No</entry>
|
|
<entry><emphasis>&true;</emphasis></entry>
|
|
<entry>このセクションを表示するかどうか</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
|
|
<itemizedlist>
|
|
<listitem><para>
|
|
必須の属性は <parameter>name</parameter> と <parameter>loop</parameter>
|
|
です。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<varname>{section}</varname> の <parameter>name</parameter> は、
|
|
英数字とアンダースコアを使って自由に命名できます。これは
|
|
<ulink url="&url.php-manual;language.variables">PHP の変数</ulink>
|
|
と同様です。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
{section} はネスト可能で、その場合の
|
|
<varname>{section}</varname> の名前はお互いにユニークである必要があります。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<parameter>loop</parameter> 属性で指定されたループ変数
|
|
(たいていは配列) は、<varname>{section}</varname>
|
|
のループ回数を決定するために使用されます。
|
|
loop の値として、整数値を渡すこともできます。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<varname>{section}</varname> 内で値を表示するには、
|
|
変数名に続けてブラケット {} で囲んだセクション名を指定します。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
ループ変数に値が存在しない場合は
|
|
<varname>{sectionelse}</varname> が実行されます。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<varname>{section}</varname> には、そのプロパティを操作するための
|
|
自身の変数があります。これらには <link linkend="language.variables.smarty.loops">
|
|
<parameter>{$smarty.section.name.property}</parameter></link>
|
|
としてアクセスできます。<quote>name</quote> は、<parameter>name</parameter>
|
|
属性の値です。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<varname>{section}</varname> のプロパティには、
|
|
<link linkend="section.property.index"><parameter>index</parameter></link>、
|
|
<link linkend="section.property.index.prev"><parameter>index_prev</parameter></link>、
|
|
<link linkend="section.property.index.next"><parameter>index_next</parameter></link>、
|
|
<link linkend="section.property.iteration"><parameter>iteration</parameter></link>、
|
|
<link linkend="section.property.first"><parameter>first</parameter></link>、
|
|
<link linkend="section.property.last"><parameter>last</parameter></link>、
|
|
<link linkend="section.property.rownum"><parameter>rownum</parameter></link>、
|
|
<link linkend="section.property.loop"><parameter>loop</parameter></link>、
|
|
<link linkend="section.property.show"><parameter>show</parameter></link>、
|
|
<link linkend="section.property.total"><parameter>total</parameter></link>
|
|
があります。
|
|
</para></listitem>
|
|
</itemizedlist>
|
|
|
|
<example>
|
|
<title>{section} でのシンプルな配列のループ</title>
|
|
<para>
|
|
配列を Smarty に <link linkend="api.assign"><varname>assign()</varname></link> します。
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$data = array(1000,1001,1002);
|
|
$smarty->assign('custid',$data);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>配列を出力するテンプレート</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* この例は $custid 配列のすべての値を表示します *}
|
|
{section name=customer loop=$custid}
|
|
id: {$custid[customer]}<br />
|
|
{/section}
|
|
<hr />
|
|
{* $custid 配列のすべての値を逆順に表示します *}
|
|
{section name=foo loop=$custid step=-1}
|
|
{$custid[foo]}<br />
|
|
{/section}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
id: 1000<br />
|
|
id: 1001<br />
|
|
id: 1002<br />
|
|
<hr />
|
|
id: 1002<br />
|
|
id: 1001<br />
|
|
id: 1000<br />
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
|
|
|
|
<example>
|
|
<title>{section} で配列を割り当てない例</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{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}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
10 12 14 16 18
|
|
<hr />
|
|
20 18 16 14 12 10
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
|
|
|
|
<example>
|
|
<title>{section} の名前</title>
|
|
<para><varname>{section}</varname> の <parameter>name</parameter>
|
|
は自由につけることができます。<ulink url="&url.php-manual;language.variables">PHP
|
|
の変数</ulink> を参照してください。これは、<varname>{section}</varname>
|
|
内のデータを参照する際に使用します。</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{section name=anything loop=$myArray}
|
|
{$myArray[anything].foo}
|
|
{$name[anything]}
|
|
{$address[anything].bar}
|
|
{/section}
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
|
|
<example>
|
|
<title>{section} での連想配列のループ</title>
|
|
<para>これは、データの連想配列を
|
|
<varname>{section}</varname> で出力する例です。
|
|
次に示すのは、配列 <parameter>$contacts</parameter>
|
|
を Smarty に渡す PHP スクリプトです。</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?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);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
|
|
<para><parameter>$contacts</parameter> を出力するテンプレート</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{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}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
<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>
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
|
|
<example>
|
|
<title>{section} での <varname>loop</varname> 変数の使用</title>
|
|
<para>この例では、<parameter>$custid</parameter>、<parameter>$name</parameter>
|
|
および <parameter>$address</parameter> にはすべて配列が割り当てられ、
|
|
その要素数は同じであるものとします。まず、Smarty に配列を割り当てる
|
|
PHP スクリプトです。</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?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);
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para><parameter>loop</parameter> 変数は、ループの回数を決定するためにのみ使用します。
|
|
<varname>{section}</varname> 内ではあらゆるテンプレート変数にアクセス可能です。</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{section name=customer loop=$custid}
|
|
<p>
|
|
id: {$custid[customer]}<br />
|
|
name: {$name[customer]}<br />
|
|
address: {$address[customer]}
|
|
</p>
|
|
{/section}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
<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>
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
|
|
|
|
|
|
<example>
|
|
<title>ネストした {section}</title>
|
|
<para>
|
|
{section} は無制限にネスト可能です。{section} をネストすることで、
|
|
多次元配列のような複雑なデータ構造にアクセスすることが可能です。
|
|
これは、配列を割り当てる <filename>.php</filename> スクリプトの例です。
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?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);
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>このテンプレートでは、<emphasis>$contact_type[customer]</emphasis>
|
|
は現在の顧客の連絡手段を格納した配列となります。</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{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}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力。
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
<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 />
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
|
|
|
|
<example>
|
|
<title>データベースを使用する {sectionelse} の例</title>
|
|
<para>データベース (ADODB や PEAR) の検索結果を Smarty に格納します。</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$sql = 'select id, name, home, cell, email from contacts '
|
|
."where name like '$foo%' ";
|
|
$smarty->assign('contacts', $db->getAll($sql));
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>データベースの結果を HTML のテーブルに出力するテンプレート</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
<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>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
|
|
<sect2 id="section.property.index">
|
|
<title>.index</title>
|
|
<para>
|
|
<parameter>index</parameter> は現在のループインデックスを表示します。
|
|
0(又は <parameter>start</parameter> 属性の値)から始まり、
|
|
1(又は <parameter>step</parameter> 属性の値)ずつ増加します。
|
|
</para>
|
|
<note>
|
|
<title>テクニカルノート</title>
|
|
<para>
|
|
<parameter>step</parameter> と <parameter>start</parameter>
|
|
属性が変更されていない場合は、セクションのプロパティ <link
|
|
linkend="section.property.iteration"><parameter>iteration</parameter></link>
|
|
と同じ動作をします。ただ、1 ではなく 0 から始まるという点が異なります。
|
|
</para>
|
|
</note>
|
|
<example>
|
|
<title>{section} の <varname>index</varname> プロパティ</title>
|
|
<para>
|
|
<note><title>ちなみに……</title>
|
|
<para><literal>$custid[customer.index]</literal> と
|
|
<literal>$custid[customer]</literal> は同じ意味です。</para>
|
|
</note>
|
|
</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{section name=customer loop=$custid}
|
|
{$smarty.section.customer.index} id: {$custid[customer]}<br />
|
|
{/section}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
0 id: 1000<br />
|
|
1 id: 1001<br />
|
|
2 id: 1002<br />
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</sect2>
|
|
|
|
|
|
<sect2 id="section.property.index.prev">
|
|
<title>.index_prev</title>
|
|
<para>
|
|
<parameter>index_prev</parameter>
|
|
は前回のループインデックスを表示します。最初のループでは-1がセットされます。
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 id="section.property.index.next">
|
|
<title>.index_next</title>
|
|
<para>
|
|
<parameter>index_next</parameter>
|
|
は次回のループインデックスを表示します。
|
|
ループの最後でもやはり現在のインデックスの次回の値を返します
|
|
(<parameter>step</parameter> 属性の設定に従います)。
|
|
</para>
|
|
|
|
<example>
|
|
<title><varname>index</varname>、<varname>index_next</varname>
|
|
および <varname>index_prev</varname> プロパティ</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$data = array(1001,1002,1003,1004,1005);
|
|
$smarty->assign('rows',$data);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>上の配列をテーブルに出力するテンプレート</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* $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>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力するテーブルは次のようになります。
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
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
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</sect2>
|
|
|
|
|
|
<sect2 id="section.property.iteration">
|
|
<title>.iteration</title>
|
|
<para>
|
|
<parameter>iteration</parameter> は現在のループが反復された回数を表示します。
|
|
</para>
|
|
<note>
|
|
<para>
|
|
<link linkend="section.property.index"><parameter>index</parameter></link>
|
|
プロパティとは異なり、これは <varname>{section}</varname> のプロパティ
|
|
<parameter>start</parameter>、<parameter>step</parameter> および <parameter>max</parameter>
|
|
の影響を受けません。
|
|
<parameter>iteration</parameter> も 1 から始まります。これは
|
|
<parameter>index</parameter> が 0 から始まるのとは異なります。<link
|
|
linkend="section.property.rownum"><parameter>rownum</parameter></link>
|
|
は <parameter>iteration</parameter> の別名で、全く同じ働きをします。
|
|
</para>
|
|
</note>
|
|
<example>
|
|
<title>セクションのプロパティ <varname>iteration</varname></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// 3000 から 3015 までの配列
|
|
$id = range(3000,3015);
|
|
$smarty->assign('arr',$id);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para><literal>$arr</literal> 配列の要素を
|
|
<literal>step=2</literal> で出力するテンプレート</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{section name=cu loop=$arr start=5 step=2}
|
|
iteration={$smarty.section.cu.iteration}
|
|
index={$smarty.section.cu.index}
|
|
id={$custid[cu]}<br />
|
|
{/section}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
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 />
|
|
]]>
|
|
</screen>
|
|
<para>
|
|
もうひとつの例は、<parameter>iteration</parameter> プロパティを使用して
|
|
5 行おきにテーブルのヘッダ部を出力します。
|
|
<link linkend="language.function.if"><varname>{if}</varname></link>
|
|
関数を mod 演算子とともに使用します。
|
|
</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
<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>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</sect2>
|
|
|
|
|
|
<sect2 id="section.property.first">
|
|
<title>.first</title>
|
|
<para>
|
|
<parameter>first</parameter> は、現在
|
|
<varname>{section}</varname> の一回目の処理を行っている場合に
|
|
&true; となります。
|
|
</para>
|
|
</sect2>
|
|
|
|
|
|
<sect2 id="section.property.last">
|
|
<title>.last</title>
|
|
<para>
|
|
<parameter>last</parameter> は、現在
|
|
<varname>{section}</varname> の最後の処理を行っている場合に
|
|
&true; となります。
|
|
</para>
|
|
<example>
|
|
<title>{section} プロパティ <varname>first</varname> と <varname>last</varname></title>
|
|
<para>
|
|
この例は <varname>$customers</varname> 配列をループし、
|
|
ループの最初でヘッダブロック、そしてループの最後でフッタブロックを出力します。
|
|
<link linkend="section.property.total"><parameter>total</parameter></link>
|
|
プロパティも使用します。
|
|
</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{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}
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</sect2>
|
|
|
|
|
|
<sect2 id="section.property.rownum">
|
|
<title>.rownum</title>
|
|
<para>
|
|
<parameter>rownum</parameter> は現在のループが反復された回数を表示します(1から開始)。
|
|
これは <link
|
|
linkend="section.property.iteration"><parameter>iteration</parameter></link>
|
|
の別名で、同じ動作をします。
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 id="section.property.loop">
|
|
<title>.loop</title>
|
|
<para>
|
|
<parameter>loop</parameter> は、この
|
|
{section} ループの最後のインデックス番号を表示します。
|
|
<varname>{section}</varname> の内部だけでなく、外部で使用することもできます。
|
|
</para>
|
|
<example>
|
|
<title>{section} プロパティ <varname>loop</varname></title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{section name=customer loop=$custid}
|
|
{$smarty.section.customer.index} id: {$custid[customer]}<br />
|
|
{/section}
|
|
There are {$smarty.section.customer.loop} customers shown above.
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
0 id: 1000<br />
|
|
1 id: 1001<br />
|
|
2 id: 1002<br />
|
|
There are 3 customers shown above.
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</sect2>
|
|
|
|
<sect2 id="section.property.show">
|
|
<title>.show</title>
|
|
<para>
|
|
<parameter>show</parameter> は、セクションのパラメータとして使用する
|
|
boolean 値です。&false; の場合はこのセクションは表示されません。
|
|
<varname>{sectionelse}</varname> があれば、それが代わりに表示されます。
|
|
</para>
|
|
<example>
|
|
<title><varname>show</varname> プロパティ</title>
|
|
<para>Boolean <varname>$show_customer_info</varname> を PHP
|
|
アプリケーションから渡し、このセクションを表示するかどうかを調整します。</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{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}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
1 id: 1000<br />
|
|
2 id: 1001<br />
|
|
3 id: 1002<br />
|
|
|
|
the section was shown.
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</sect2>
|
|
|
|
<sect2 id="section.property.total">
|
|
<title>.total</title>
|
|
<para>
|
|
<parameter>total</parameter> は <varname>{section}</varname>
|
|
がループしたトータル回数を表示します。これは
|
|
<varname>{section}</varname> の内部だけでなく外部でも使うことができます。
|
|
</para>
|
|
<example>
|
|
<title><varname>total</varname> プロパティの例</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{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.
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<para>
|
|
<link linkend="language.function.foreach"><varname>{foreach}</varname></link>
|
|
および
|
|
<link linkend="language.variables.smarty.loops"><parameter>$smarty.section</parameter></link>
|
|
も参照してください。
|
|
</para>
|
|
</sect2>
|
|
</sect1>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"../../../../manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|