|
|

|
{section},{sectionelse}
{section}
adalah untuk mengulang melalui array data,
tidak seperti {foreach}
yang dipakai untuk mengulang melalui
satu array asosiatif.
Setiap {section} tag harus dipasangkan dengan
penutup{/section} tag.
Atribut yang diperlukan adalah name dan loop.
name dari {section} bisa berupa
apapun yang anda suka, terdiri dari huruf, angka dan garis bawah, seperti
variabel PHP.
{section} dapat diulang, dan nama {section} yang diulang
harus unik dari yang lainnya.
Atribut loop, biasanya sebuah array nilai, menentukan
jumlah berapa kali {section} akan mengulang. Anda juga
dapat mengirimkan integer sebagai nilai pengulangan.
Ketika mencetak variabel di dalam {section},
{section} name harus diberikan di
sebelah nama variabel dalam [kurung kotak].
{sectionelse} dijalankan saat tidak ada lagi nilai
dalam variabel loop.
{section} juga memiliki variabelnya sendiri yang
menangani properti {section}.
Properti ini dapat diakses sebagai:
{$smarty.section.name.property}
di mana "name" adalah atribut name.
properti {section} adalah
index,
index_prev,
index_next,
iteration,
first,
last,
rownum,
loop,
show,
total.
Teladan 7-30. Mengulang array sederhana dengan {section}
assign() array ke Smarty
<?php $data = array(1000,1001,1002); $smarty->assign('custid',$data); ?>
|
Template yang menampilkan array {* 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} |
Contoh di atas akan menampilkan:
id: 1000<br />
id: 1001<br />
id: 1002<br />
<hr />
id: 1002<br />
id: 1001<br />
id: 1000<br /> |
|
Teladan 7-31. {section} tanpa array yang ditempati {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} |
Contoh di atas akan menampilkan:
10 12 14 16 18
<hr />
20 18 16 14 12 10 |
|
Teladan 7-32. Penamaan {section} name dari {section} bisa apa
saja sesuai yang anda inginkan, lihat
variabel PHP.
Ini dipakai untuk mereferensi data di dalam {section}. {section name=anything loop=$myArray}
{$myArray[anything].foo}
{$name[anything]}
{$address[anything].bar}
{/section} |
|
Teladan 7-33. Pengulangan array asosiatif dengan {section} Ini adalah contoh pencetakan array data asosiatif dengan
{section}. Berikut adalah naskah php untuk menempatkan
array $contacts ke Smarty.
<?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); ?>
|
Template untuk menampilkan $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} |
Contoh di atas akan menampilkan:
<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> |
|
Teladan 7-34. {section} mendemonstrasikan variabel loop Contoh ini mengasumsikan bahwa $custid, $name
dan $address adalah semua array yang berisi jumlah
nilai yang sama. Pertama naskah php menempatkan array ke Smarty.
<?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);
?>
|
Variabel loop hanya menentukan jumlah berapa kali
untuk mengulang. Anda dapat mengakses variabel MANAPUN dari template di dalam
{section} {section name=customer loop=$custid}
<p>
id: {$custid[customer]}<br />
name: {$name[customer]}<br />
address: {$address[customer]}
</p>
{/section} |
Contoh di atas akan menampilkan:
<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> |
|
Teladan 7-35. {section} yang berulang
{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 .php yang
menempatkan array.
<?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);
?>
|
Dalam contoh ini, $contact_type[customer] adalah
sebuah array tipe kontak untuk kustomer saat ini. {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} |
Contoh di atas akan menampilkan:
<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 /> |
|
Teladan 7-36. Contoh database dengan {sectionelse} Hasil pencarian database (misal ADODB atau PEAR) ditempatkan ke Smarty
<?php $sql = 'select id, name, home, cell, email from contacts ' ."where name like '$foo%' "; $smarty->assign('contacts', $db->getAll($sql)); ?>
|
Template yang menampilkan hasil database dalam tabel 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">Tidak ada item yang ditemukan</td></tr>
{/section}
</table> |
|
.index
index berisi indeks array saat ini, dimulai dengan nol
atau atribut start bila diberikan. Ia bertambah satu
atau dengan atribut step bila diberikan.
Catatan Teknis:
Jika properti step dan start
tidak diubah, maka pekerjaan ini sama seperti properti iteration,
kecuali ia dimulai dengan nol daripada satu.
Teladan 7-37. {section} index property
FYI: $custid[customer.index] dan
$custid[customer] adalah sama.
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section} |
Contoh di atas akan menampilkan:
0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br /> |
|
.index_prev
index_prev adalah indeks pengulangan sebelumnya.
Pada pengulangan pertama, ini disetel -1.
.index_next
index_next adalah indeks pengulangan berikutnya.
Pada pengulangan terakhir, ini masih satu lagi daripada indeks saat ini,
memperhatikan setelan atribut step, jika diberikan.
Teladan 7-38. properti index, index_next
dan index_prev
<?php $data = array(1001,1002,1003,1004,1005); $smarty->assign('rows',$data); ?>
|
Template untuk menampilkan array di atas dalam sebuah tabel {* $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> |
Contoh di atas akan menampilkan tabel yang berisi sebagai berikut:
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
iteration berisi iterasi pengulangan saat ini dan
dimulai dari satu.
Catatan:
Ini tidak dipengaruhi oleh properti {section}
start, step dan max,
tidak seperti properti
index.
iteration juga dimulai dengan satu daripada nol
tidak seperti index. rownum
adalah alias untuk iteration, keduanya sama.
Teladan 7-39. Properti iteration dari seksi
<?php // array of 3000 to 3015 $id = range(3000,3015); $smarty->assign('arr',$id); ?>
|
Template untuk menampilkan setiap elemen lain array $arr
dengan 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} |
Contoh di atas akan menampilkan:
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 /> |
Contoh lain yang menggunakan properti iteration untuk
mengeluarkan blok header tabel setiap lima baris.
Menggunakan fungsi {if}
dengan operator 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> |
|
.first
first disetel TRUE jika iterasi
{section} saat ini adalah yang pertama.
.last
last disetel TRUE jika iterasi seksi saat ini
adalah yang terakhir.
Teladan 7-40. properti {section} first dan last
Contoh ini mengulang array $customers, mengeluarkan
blok header pada iterasi pertama dan terakhir mengeluarkan blok footer.
Juga menggunakan properti
total.
{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} |
|
.rownum
rownum berisi iterasi pengulangan saat ini,
dimulai dengan satu. Ini adalah nama lain dari iterasi,
cara kerjanya sama persis.
.loop
loop berisi angka indeks terakhir yang mengulang
{section}. Ini bisa dipakai di dalam atau setelah {section}.
Teladan 7-41. properti {section} loop {section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
Ada {$smarty.section.customer.loop} kustomer yang ditampilkan di atas. |
Contoh di atas akan menampilkan:
0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />
Ada 3 kustomer yang ditampilkan di atas. |
|
.show
show dipakai sebagai parameter ke seksi dan berupa
nilai boolean. Jika FALSE, seksi tidak akan ditampilkan. Jika terdapat
{sectionelse}, akan ditampilkan sebagai alternatif.
Teladan 7-42. show properti Boolean $show_customer_info sudah dikirimkan dari
aplikasi PHP, untuk mengatur apakah seksi ditampilkan atau tidak. {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} |
Contoh di atas akan menampilkan:
1 id: 1000<br />
2 id: 1001<br />
3 id: 1002<br />
the section was shown. |
|
.total
total berisi jumlah iterasi yang akan
{section} mengulangnya. Ini bisa dipakai di dalam atau setelah
{section}.
Teladan 7-43. total contoh properti {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. |
|
Lihat juga {foreach}
dan
$smarty.section.
|
|
|