section, sectionelse function

section,sectionelse function:

section tags must appear in pairs. The name and loop attributes must be set.

The name can be any combination of letters, numbers and underscores. It can be nested but the nested name must be unique.

Determined by the variable loop (usually an array) The number of times the loop is executed.

#When needed When outputting a variable within a section loop, the name variable enclosed in square brackets must be added after the variable.

sectionelse is executed when the loop variable has no value.

eg1:

test.php:

##$smarty->assign('custid',array(1000, 10001,10002));

test.html:

{section name=customer loop=$custid}id: {$custid[customer]}

{/section}


Output:id: 1000

id: 1001< br>
id: 1002



#eg2: (Traverse multi-dimensional array)

test.php:

##$smarty->assign('contacts', array(

array('custid'=> 1000,'name'=>'smile1','address'=>'Hefei'),

array('custid'=>1000,'name'=>'smile2','address'= >'Shanghai'),array('custid'=>1000,'name'=>'smile3','address'=>'Beijing'),
));


test.html:

##{section name=customer loop=$contacts}id: {$contacts[customer].custid}< ;br>

name: {$contacts[customer].name}

address: {$contacts[customer].address}
{/section}



Output:

id: 1000name: smile1address: Hefeiid: 1000
name: smile2
address : Shanghai
id: 1000
name: smile3
address: Beijing



##eg3:(sectionelse demo)


test.php:

$smarty->assign('custid',array());

test.html:

{section name=customer loop=$custid}
id: {$custid[customer]}

{sectionelse}
there are no values in $custid.
{/section}

输出:

there are no values in $custid.

Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!