mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-10 01:01:45 +01:00
827 lines
23 KiB
XML
827 lines
23 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!-- $Revision$ -->
|
|
<sect1 id="language.function.section">
|
|
<title>{section},{sectionelse}</title>
|
|
<para>
|
|
<varname>{section}</varname>
|
|
adalah untuk mengulang melalui <emphasis role="bold">array data</emphasis>,
|
|
tidak seperti <link linkend="language.function.foreach"><varname>{foreach}</varname></link>
|
|
yang dipakai untuk mengulang melalui
|
|
<emphasis role="bold">satu array asosiatif</emphasis>.
|
|
Setiap <varname>{section}</varname> tag harus dipasangkan dengan
|
|
penutup<varname>{/section}</varname> tag.
|
|
</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>Nama Atribut</entry>
|
|
<entry>Tipe</entry>
|
|
<entry>Diperlukan</entry>
|
|
<entry>Default</entry>
|
|
<entry>Deskripsi</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>name</entry>
|
|
<entry>string</entry>
|
|
<entry>Ya</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>Nama seksi</entry>
|
|
</row>
|
|
<row>
|
|
<entry>loop</entry>
|
|
<entry>mixed</entry>
|
|
<entry>Ya</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>Nilai untuk menentukan sumber iterasi pengulangan</entry>
|
|
</row>
|
|
<row>
|
|
<entry>start</entry>
|
|
<entry>integer</entry>
|
|
<entry>Tidak</entry>
|
|
<entry><emphasis>0</emphasis></entry> <entry>Posisi indeks
|
|
di mana seksi akan diulang. Jika nilai negatif, awal
|
|
posisi dihitung dari akhir array. Sebagai contoh,
|
|
jika ada tujuh nilai dalam pengulangan array dan awalnya
|
|
adalah -2, indeks awal adalah 5. Nilai yang tidak benar
|
|
(nilai di luar panjang pengulangan array) otomatis
|
|
dipotong ke nilai terdekat yang benar.</entry>
|
|
</row>
|
|
<row>
|
|
<entry>step</entry>
|
|
<entry>integer</entry>
|
|
<entry>Tidak</entry>
|
|
<entry><emphasis>1</emphasis></entry>
|
|
<entry>Nilai step akan dipakai untuk melewati pengulangan array.
|
|
Sebagai contoh, step=2 akan berulang pada indeks 0,2,4, dst.
|
|
Jika step negatif, ia akan mundur kembali melewati array.</entry>
|
|
</row>
|
|
<row>
|
|
<entry>max</entry>
|
|
<entry>integer</entry>
|
|
<entry>Tidak</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>Menyetel angka maksimum berapa kali seksi akan mengulang.</entry>
|
|
</row>
|
|
<row>
|
|
<entry>show</entry>
|
|
<entry>boolean</entry>
|
|
<entry>Tidak</entry>
|
|
<entry><emphasis>&true;</emphasis></entry>
|
|
<entry>Menentukan apakan menampilkan seksi ini atau tidak</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
|
|
<itemizedlist>
|
|
<listitem><para>
|
|
Atribut yang diperlukan adalah <parameter>name</parameter> dan <parameter>loop</parameter>.
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<parameter>name</parameter> dari <varname>{section}</varname> bisa berupa
|
|
apapun yang anda suka, terdiri dari huruf, angka dan garis bawah, seperti
|
|
<ulink url="&url.php-manual;language.variables">variabel PHP</ulink>.
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
{section} dapat diulang, dan nama <varname>{section}</varname> yang diulang
|
|
harus unik dari yang lainnya.
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
Atribut <parameter>loop</parameter>, biasanya sebuah array nilai, menentukan
|
|
jumlah berapa kali <varname>{section}</varname> akan mengulang. Anda juga
|
|
dapat mengirimkan integer sebagai nilai pengulangan.
|
|
</para></listitem>
|
|
|
|
<listitem><para>Ketika mencetak variabel di dalam <varname>{section}</varname>,
|
|
<varname>{section}</varname> <parameter>name</parameter> harus diberikan di
|
|
sebelah nama variabel dalam [kurung kotak].
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<varname>{sectionelse}</varname> dijalankan saat tidak ada lagi nilai
|
|
dalam variabel loop.
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<varname>{section}</varname> juga memiliki variabelnya sendiri yang
|
|
menangani properti <varname>{section}</varname>.
|
|
Properti ini dapat diakses sebagai: <link linkend="language.variables.smarty.loops">
|
|
<parameter>{$smarty.section.name.property}</parameter></link>
|
|
di mana <quote>name</quote> adalah atribut <parameter>name</parameter>.
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
properti <varname>{section}</varname> adalah
|
|
<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>Mengulang array sederhana dengan {section}</title>
|
|
<para>
|
|
<link linkend="api.assign"><varname>assign()</varname></link> array ke Smarty
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$data = array(1000,1001,1002);
|
|
$smarty->assign('custid',$data);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>Template yang menampilkan array</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* contoh ini akan mengeluarkan semua nilai dari array $custid *}
|
|
{section name=customer loop=$custid}
|
|
id: {$custid[customer]}<br />
|
|
{/section}
|
|
<hr />
|
|
{* mengeluarkan semua nilai array $custid secara terbalik *}
|
|
{section name=foo loop=$custid step=-1}
|
|
{$custid[foo]}<br />
|
|
{/section}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
Contoh di atas akan menampilkan:
|
|
</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} tanpa array yang ditempati</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>
|
|
Contoh di atas akan menampilkan:
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
10 12 14 16 18
|
|
<hr />
|
|
20 18 16 14 12 10
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
|
|
|
|
<example>
|
|
<title>Penamaan {section}</title>
|
|
<para><parameter>name</parameter> dari <varname>{section}</varname> bisa apa
|
|
saja sesuai yang anda inginkan, lihat
|
|
<ulink url="&url.php-manual;language.variables">variabel PHP</ulink>.
|
|
Ini dipakai untuk mereferensi data di dalam <varname>{section}</varname>.</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{section name=anything loop=$myArray}
|
|
{$myArray[anything].foo}
|
|
{$name[anything]}
|
|
{$address[anything].bar}
|
|
{/section}
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
|
|
<example>
|
|
<title>Pengulangan array asosiatif dengan {section}</title>
|
|
<para>Ini adalah contoh pencetakan array data asosiatif dengan
|
|
<varname>{section}</varname>. Berikut adalah naskah php untuk menempatkan
|
|
array <parameter>$contacts</parameter> ke Smarty.</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>Template untuk menampilkan <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>
|
|
Contoh di atas akan menampilkan:
|
|
</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} mendemonstrasikan variabel <varname>loop</varname></title>
|
|
<para>Contoh ini mengasumsikan bahwa <parameter>$custid</parameter>, <parameter>$name</parameter>
|
|
dan <parameter>$address</parameter> adalah semua array yang berisi jumlah
|
|
nilai yang sama. Pertama naskah php menempatkan array ke Smarty.</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>Variabel <parameter>loop</parameter> hanya menentukan jumlah berapa kali
|
|
untuk mengulang. Anda dapat mengakses variabel MANAPUN dari template di dalam
|
|
<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>
|
|
Contoh di atas akan menampilkan:
|
|
</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} yang berulang</title>
|
|
<para>
|
|
{section} dapat diulang sedalam yang anda suka. Dengan {section} yang
|
|
diulang, anda bisa mengakses struktur data yang kompleks, seperti array
|
|
multi dimensi. Ini adalah contoh naskah <filename>.php</filename> yang
|
|
menempatkan array.
|
|
</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>Dalam contoh ini, <emphasis>$contact_type[customer]</emphasis> adalah
|
|
sebuah array tipe kontak untuk kustomer saat ini.</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>
|
|
Contoh di atas akan menampilkan:
|
|
</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>Contoh database dengan {sectionelse}</title>
|
|
<para>Hasil pencarian database (misal ADODB atau PEAR) ditempatkan ke 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>Template yang menampilkan hasil database dalam tabel 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">Tidak ada item yang ditemukan</td></tr>
|
|
{/section}
|
|
</table>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
|
|
<sect2 id="section.property.index">
|
|
<title>.index</title>
|
|
<para>
|
|
<parameter>index</parameter> berisi indeks array saat ini, dimulai dengan nol
|
|
atau atribut <parameter>start</parameter> bila diberikan. Ia bertambah satu
|
|
atau dengan atribut <parameter>step</parameter> bila diberikan.
|
|
</para>
|
|
<note>
|
|
<title>Catatan Teknis</title>
|
|
<para>
|
|
Jika properti <parameter>step</parameter> dan <parameter>start</parameter>
|
|
tidak diubah, maka pekerjaan ini sama seperti properti <link
|
|
linkend="section.property.iteration"><parameter>iteration</parameter></link>,
|
|
kecuali ia dimulai dengan nol daripada satu.
|
|
</para>
|
|
</note>
|
|
<example>
|
|
<title>{section} <varname>index</varname> property</title>
|
|
<para>
|
|
<note><title>FYI</title>
|
|
<para><literal>$custid[customer.index]</literal> dan
|
|
<literal>$custid[customer]</literal> adalah sama.</para>
|
|
</note>
|
|
</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{section name=customer loop=$custid}
|
|
{$smarty.section.customer.index} id: {$custid[customer]}<br />
|
|
{/section}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
Contoh di atas akan menampilkan:
|
|
</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> adalah indeks pengulangan sebelumnya.
|
|
Pada pengulangan pertama, ini disetel -1.
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 id="section.property.index.next">
|
|
<title>.index_next</title>
|
|
<para>
|
|
<parameter>index_next</parameter> adalah indeks pengulangan berikutnya.
|
|
Pada pengulangan terakhir, ini masih satu lagi daripada indeks saat ini,
|
|
memperhatikan setelan atribut <parameter>step</parameter>, jika diberikan.
|
|
</para>
|
|
|
|
<example>
|
|
<title>properti <varname>index</varname>, <varname>index_next</varname>
|
|
dan <varname>index_prev</varname> </title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$data = array(1001,1002,1003,1004,1005);
|
|
$smarty->assign('rows',$data);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>Template untuk menampilkan array di atas dalam sebuah tabel</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* $rows[row.index] dan $rows[row] adalah identik dalam pengertian *}
|
|
<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>
|
|
Contoh di atas akan menampilkan tabel yang berisi sebagai berikut:
|
|
</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> berisi iterasi pengulangan saat ini dan
|
|
dimulai dari satu.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Ini tidak dipengaruhi oleh properti <varname>{section}</varname>
|
|
<parameter>start</parameter>, <parameter>step</parameter> dan <parameter>max</parameter>,
|
|
tidak seperti properti
|
|
<link linkend="section.property.index"><parameter>index</parameter></link>.
|
|
<parameter>iteration</parameter> juga dimulai dengan satu daripada nol
|
|
tidak seperti <parameter>index</parameter>. <link
|
|
linkend="section.property.rownum"><parameter>rownum</parameter></link>
|
|
adalah alias untuk <parameter>iteration</parameter>, keduanya sama.
|
|
</para>
|
|
</note>
|
|
<example>
|
|
<title>Properti <varname>iteration</varname> dari seksi </title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// array of 3000 to 3015
|
|
$id = range(3000,3015);
|
|
$smarty->assign('arr',$id);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>Template untuk menampilkan setiap elemen lain array <literal>$arr</literal>
|
|
dengan <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>
|
|
Contoh di atas akan menampilkan:
|
|
</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>
|
|
Contoh lain yang menggunakan properti <parameter>iteration</parameter> untuk
|
|
mengeluarkan blok header tabel setiap lima baris.
|
|
Menggunakan fungsi <link linkend="language.function.if"><varname>{if}</varname></link>
|
|
dengan operator 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> disetel &true; jika iterasi
|
|
<varname>{section}</varname> saat ini adalah yang pertama.
|
|
</para>
|
|
</sect2>
|
|
|
|
|
|
<sect2 id="section.property.last">
|
|
<title>.last</title>
|
|
<para>
|
|
<parameter>last</parameter> disetel &true; jika iterasi seksi saat ini
|
|
adalah yang terakhir.
|
|
</para>
|
|
<example>
|
|
<title>properti {section} <varname>first</varname> dan <varname>last</varname></title>
|
|
<para>
|
|
Contoh ini mengulang array <varname>$customers</varname>, mengeluarkan
|
|
blok header pada iterasi pertama dan terakhir mengeluarkan blok footer.
|
|
Juga menggunakan properti
|
|
<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>kustomer</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} kustomer</td></tr>
|
|
</table>
|
|
{/if}
|
|
{/section}
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</sect2>
|
|
|
|
|
|
<sect2 id="section.property.rownum">
|
|
<title>.rownum</title>
|
|
<para>
|
|
<parameter>rownum</parameter> berisi iterasi pengulangan saat ini,
|
|
dimulai dengan satu. Ini adalah nama lain dari <link
|
|
linkend="section.property.iteration"><parameter>iterasi</parameter></link>,
|
|
cara kerjanya sama persis.
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 id="section.property.loop">
|
|
<title>.loop</title>
|
|
<para>
|
|
<parameter>loop</parameter> berisi angka indeks terakhir yang mengulang
|
|
{section}. Ini bisa dipakai di dalam atau setelah <varname>{section}</varname>.
|
|
</para>
|
|
<example>
|
|
<title>properti {section} <varname>loop</varname></title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{section name=customer loop=$custid}
|
|
{$smarty.section.customer.index} id: {$custid[customer]}<br />
|
|
{/section}
|
|
Ada {$smarty.section.customer.loop} kustomer yang ditampilkan di atas.
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
Contoh di atas akan menampilkan:
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
0 id: 1000<br />
|
|
1 id: 1001<br />
|
|
2 id: 1002<br />
|
|
Ada 3 kustomer yang ditampilkan di atas.
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</sect2>
|
|
|
|
<sect2 id="section.property.show">
|
|
<title>.show</title>
|
|
<para>
|
|
<parameter>show</parameter> dipakai sebagai parameter ke seksi dan berupa
|
|
nilai boolean. Jika &false;, seksi tidak akan ditampilkan. Jika terdapat
|
|
<varname>{sectionelse}</varname>, akan ditampilkan sebagai alternatif.
|
|
</para>
|
|
<example>
|
|
<title><varname>show</varname> properti </title>
|
|
<para>Boolean <varname>$show_customer_info</varname> sudah dikirimkan dari
|
|
aplikasi PHP, untuk mengatur apakah seksi ditampilkan atau tidak.</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}
|
|
seksi ditampilkan.
|
|
{else}
|
|
seksi tidak ditampilkan.
|
|
{/if}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
Contoh di atas akan menampilkan:
|
|
</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> berisi jumlah iterasi yang akan
|
|
<varname>{section}</varname> mengulangnya. Ini bisa dipakai di dalam atau setelah
|
|
<varname>{section}</varname>.
|
|
</para>
|
|
<example>
|
|
<title><varname>total</varname> contoh properti</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{section name=customer loop=$custid step=2}
|
|
{$smarty.section.customer.index} id: {$custid[customer]}<br />
|
|
{/section}
|
|
Ada {$smarty.section.customer.total} kustomer yang ditampilkan di atas.
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<para>
|
|
Lihat juga <link linkend="language.function.foreach"><varname>{foreach}</varname></link>
|
|
dan
|
|
<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
|
|
-->
|