section,sectionelse函数

section,sectionelse函数:

section 标签必须成对出现. 必须设置 name 和 loop 属性.

名称可以是包含字母、数字和下划线的任意组合. 可以嵌套但必须保证嵌套的 name 唯一. 

变量 loop (通常是数组)决定循环执行的次数. 

当需要在 section 循环内输出变量时,必须在变量后加上中括号包含着的 name 变量. 

sectionelse 当 loop 变量无值时被执行. 

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:(遍历多维数组)

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:

{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{sectionelse}
there are no values in $custid.
{/section}

输出:

there are no values in $custid.

Weiter lernen
||
<?php echo "section,sectionelse函数";
einreichenCode zurücksetzen