section,sectionelse函數(shù)
section,sectionelse函數(shù):
section 標簽必須成對出現(xiàn). 必須設置 name 和 loop 屬性.
名稱可以是包含字母、數(shù)字和下劃線的任意組合. 可以嵌套但必須保證嵌套的 name 唯一.?
變量 loop (通常是數(shù)組)決定循環(huán)執(zhí)行的次數(shù).?
當需要在 section 循環(huán)內輸出變量時,必須在變量后加上中括號包含著的 name 變量.?
sectionelse 當 loop 變量無值時被執(zhí)行.?
eg1:
test.php:
$smarty->assign('custid',array(1000,10001,10002));
test.html:
{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{/section}
輸出:
id: 1000<br>
id: 1001<br>
id: 1002<br>
eg2:(遍歷多維數(shù)組)
test.php:
$smarty->assign('contacts', array(
? ?array('custid'=>1000,'name'=>'smile1','address'=>'合肥'),
? ?array('custid'=>1000,'name'=>'smile2','address'=>'上海'),
? ?array('custid'=>1000,'name'=>'smile3','address'=>'北京'),
));
test.html:
{section name=customer loop=$contacts}
id: {$contacts[customer].custid}<br>
name: {$contacts[customer].name}<br>
address: {$contacts[customer].address}<br>
{/section}
輸出:
id: 1000
name: smile1
address: 合肥
id: 1000
name: smile2
address: 上海
id: 1000
name: smile3
address: 北京
eg3:(sectionelse 演示?)
test.php:
$smarty->assign('custid',array());
test.html:
{部分名稱=客戶循環(huán)=$custid}
id: {$custid[客戶] }<br>
{sectionelse}
$custid 中沒有值。
{/section}
輸出:
$custid 中沒有值。