{section}鍙互寰幆閬嶅巻
杩炵画鏁板瓧绱㈠紩鐨勬暟缁,
鍖哄埆浜{foreach}
鍙互寰幆浠绘剰鍏宠仈鏁扮粍.
姣忎釜{section}鏍囩閮藉繀椤绘湁涓涓尮閰嶇殑{/section}鍏抽棴鏍囩銆
Note
{foreach}
鍙互鍋氬埌浠讳綍{section}鍋氬埌鐨勫姛鑳, 鑰屼笖鏇寸畝鍗曞拰鏈夋洿娓呮櫚鐨勮娉曘備竴鑸洿鎺ㄨ崘浣跨敤{foreach}璇硶.
Note
{section}涓嶈兘鐢ㄤ簬寰幆鍏宠仈鏁扮粍锛屽畠浠呰兘寰幆鏁板瓧绱㈠紩鐨勩佽繛缁笅鏍囩殑 (0,1,2,...)鏁扮粍銆
瑕佸惊鐜叧鑱旀暟缁勶紝璇蜂娇鐢{foreach}銆
| 鍙傛暟鍚嶇О | 绫诲瀷 | 蹇呴夊弬鏁 | 榛樿鍊 | 璇存槑 |
|---|---|---|---|---|
| name | string | Yes | n/a | section鐨勫悕绉 |
| loop | mixed | Yes | n/a | 鐢ㄤ簬寰幆鐨勫 |
| start | integer | No | 0 | 璁剧疆寮濮嬪惊鐜殑涓嬫爣銆傚鏋滆缃垚璐熷硷紝鍒欎細浠庢暟缁勭殑缁撴潫浣嶇疆寮濮嬨 姣斿璇达紝濡傛灉鏁扮粍涓湁7涓厓绱狅紝璁剧疆璇ュ间负-2锛屽垯寰幆灏嗕粠涓嬫爣5寮濮嬨 璁剧疆浜嗕笉姝g‘鐨勫硷紙姣斿璇村湪鏁扮粍闀垮害浠ュ鐨勫硷級閭d箞浼氳嚜鍔ㄨ绠椾负鏈鎺ヨ繎鐨勫笺 |
| step | integer | No | 1 | 寰幆鐨勬闀垮笺傛瘮濡傦紝step=2灏嗗惊鐜笅鏍0,2,4,绛夈 濡傛灉step鍊艰缃垚璐熸暟锛岄偅涔堝皢浠庢渶鍚庡紑濮嬭绠楁闀裤 |
| max | integer | No | n/a | 璁剧疆鏈澶х殑寰幆娆℃暟銆 |
| show | boolean | No | TRUE |
鏄惁鏄剧ず寰幆鍐呭 |
鍙夋爣璁:
| 鍚嶇О | 璇存槑 |
|---|---|
| nocache | 鍏抽棴{section}缂撳瓨 |
name鍜loop鏄繀椤荤殑鍙傛暟銆{section}鐨name鍙互鏄换鎰忓瓧绗︼紝濡傛暟瀛椼佸瓧姣嶆垨涓嬪垝绾跨瓑锛屽拰 PHP 鍙橀噺涓鏍风殑鍛藉悕瑙勫垯銆{section}鍙互宓屽锛岃屼笖宓屽鐨
{section}鍚嶇О蹇呴』鍞竴銆loop涓鑸槸鏁扮粍锛屽喅瀹氫簡{section}鐨勫惊鐜鏁般 鍚屾椂浣犱篃鍙互浼犻掍竴涓暣鏁版寚瀹氬惊鐜鏁般褰撳湪
{section}鍐呮樉绀哄彉閲忔椂,{section}鐨name蹇呴』缁欏彉閲忓悕绉板姞涓奫鏂规嫭鍙穄.濡傛灉loop灞炴т负绌猴紝
{sectionelse}灏嗘墽琛屻{section}鍚屾椂杩樻湁鑷繁鐨勫睘鎬с 杩欎簺灞炴ч兘鏄氳繃:{$smarty.section.name.property}鏉ヤ娇鐢紝鍏朵腑鈥name鈥鏄name灞炴.{section}鐨勫唴缃睘鎬ф湁锛index,index_prev,index_next,iteration,first,last,rownum,loop,show,total.
Example聽7.63.聽绠鍗曠殑{section}渚嬪瓙
assign()璧嬪间竴涓暟缁
<?php
$data = array(1000,1001,1002);
$smarty->assign('custid',$data);
?>
妯℃澘灏嗚緭鍑鸿鏁扮粍
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
{section customer $custid} {* short-hand *}
id: {$custid[customer]}<br />
{/section}
<hr />
{* print out all the values of the $custid array reversed *}
{section name=foo loop=$custid step=-1}
{section foo $custid step=-1} {* short-hand *}
{$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.64.聽{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.65.聽缁檣section}璁剧疆鍚嶇О
{section}鐨 name灞炴у彲浠ユ槸浠绘剰瀛楃锛岃鍙傝PHP 鍙橀噺瀹氫箟.
瀹冩槸鐢ㄤ簬寮曠敤{section}鐨勬暟鎹.
{section name=anything loop=$myArray}
{$myArray[anything].foo}
{$name[anything]}
{$address[anything].bar}
{/section}
Example聽7.66.聽{section}涓娇鐢ㄥ叧鑱旀暟缁
涓嬮潰鏄娇鐢{section}鏉ヨ緭鍑哄叧鑱旀暟缁勭殑渚嬪瓙銆
杩欓噷鏄湪PHP浠g爜涓祴鍊$contacts 鏁扮粍鍒癝marty銆
<?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.67.聽{section}鐨loop灞炴х殑婕旂ず渚嬪瓙
渚嬪瓙鍋囧畾$custid, $name
鍜 $address涓変釜鏁扮粍涓搴旂殑鍊奸兘鏈夌潃鐩稿悓鐨勬暟瀛椾笅鏍囥
棣栧厛浠嶱HP浠g爜涓祴鍊煎埌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);
?>
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.68.聽{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.69.聽{sectionelse}鐨勬暟鎹簱渚嬪瓙
鏁版嵁搴撴煡鎵剧殑缁撴灉(濡 ADODB 鎴 PEAR) 浼犻掑埌 Smarty
<?php
$sql = 'select id, name, home, cell, email from contacts '
."where name like '$foo%' ";
$smarty->assign('contacts', $db->getAll($sql));
?>
妯℃澘灏嗕互琛ㄦ牸褰㈠紡鏄剧ず鏁版嵁缁撴灉
<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閮芥病鏈夎鎸囧畾锛
閭d箞瀹冧細鍜iteration灞炴у緢鍍, 鍙笉杩囧畠鏄粠0寮濮嬶紝鑰宨teration鏄粠1寮濮.
Example聽7.70.聽{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鏄笅涓娆″惊鐜殑绱㈠紩鍊笺
鍦ㄦ渶鍚庝竴娆″惊鐜椂锛屽畠浼氭瘮褰撳墠绱㈠紩鍔1锛屾垨鑰呭姞涓婃寚瀹氱殑step灞炴у笺
Example聽7.71.聽index, index_next
鍜 index_prev 灞炴
<?php
$data = array(1001,1002,1003,1004,1005);
$smarty->assign('rows',$data);
?>
鍦ㄨ〃鏍间腑鏄剧ず鏁扮粍
{* $rows[row.index] and $rows[row] are identical in meaning *}
<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鏄綋鍓嶇殑寰幆娆℃暟锛屼粠1寮濮嬨
Note
瀹冨拰index涓嶅悓锛屼笉浼氬彈鍒{section} 鐨勫睘鎬
start, step 鍜 max绛夊奖鍝.
鑰屼笖鍜index涓嶅悓鐨勬槸锛iteration杩樻槸浠1寮濮嬭绠楃殑銆 rownum鏄
iteration鐨勫埆鍚,瀹冧滑鏄竴鏍风殑銆
Example聽7.72.聽iteration灞炴
<?php
// array of 3000 to 3015
$id = range(3000,3015);
$smarty->assign('arr',$id);
?>
妯℃澘灏嗘寜step=2鏉ユ樉绀$arr鐨勬暟缁勫厓绱
{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灞炴ф潵鏄剧ず琛ㄦ牸锛
骞朵笖姣忎簲琛屾樉绀轰竴娆¤〃澶淬
<table>
{section name=co loop=$contacts}
{if $smarty.section.co.iteration is div by 5}
<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>
涓涓敤iteration灞炴ф潵浜ゆ浛鏄剧ず鏂囩珷姣忎笁琛岄鑹茬殑渚嬪瓙
<table>
{section name=co loop=$contacts}
{if $smarty.section.co.iteration is even by 3}
<span style="color: #ffffff">{$contacts[co].name}</span>
{else}
<span style="color: #dddddd">{$contacts[co].name}</span>
{/if}
{/section}
</table>
Note
"is div by"璇硶鏄疨HP鍙栨ā杩愮畻鐨勪竴涓彉绉嶃傚彇妯¤繍绠{if $smarty.section.co.iteration % 5 == 1}涔熸槸鍙敤鐨勩
Note
浣犺繕鍙互鐢"is odd by"鏉ュ弽杞氦鏇裤
濡傛灉褰撳墠鐨勫惊鐜槸鏈鍚庝竴娆★紝閭d箞last灏嗕负 TRUE銆
Example聽7.73.聽{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.74.聽{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鏄竴涓竷灏斿煎弬鏁般傚鏋滆缃负FALSE锛宻ection灏嗕笉浼氳鏄剧ず銆
濡傛灉鏈{sectionelse}鏄剧ず锛屽畠浠皢琚氦鏇挎樉绀恒
Example聽7.75.聽show 灞炴
甯冨皵鍊 $show_customer_info 鍙互鍦≒HP绋嬪簭璧嬪煎苟浼犻掑埌妯℃澘涓紝
鍙互鎺у埗section鐨勬樉绀轰笌鍚︺
{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.76.聽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},
{for},
{while}
鍜
$smarty.section.
