smarty section 用法

参考了smarty的官方文档,不过是翻译了部分,也加了点注释,个人感觉是比较有用的。欢迎交流。

1、循环一个简单的一维数组:
Example 7-30. Looping a simple array with {section}

$data = array(1000,1001,1002);
$smarty->assign(‘custid’,$data);
?>

//customer 和下面的foo可以随便命名,作用其实仅仅是一个index下标,用来引用数组中的元素
{section name=customer loop=$custid}
id: {$custid[customer]}

{/section}

{section name=foo loop=$custid step=-1}
{$custid[foo]}

{/section}

// 输出
id: 1000

id: 1001

id: 1002

id: 1002

id: 1001

id: 1000

2、不用assign数组直接在smarty中循环:
Example 7-31. {section} without an assigned array

//特别地设置了start,step属性用来控制循环
//$smarty.section.section的名字.index是一个特殊变量,用来显示当前循环的位置
{section name=foo start=10 loop=20 step=2}
{$smarty.section.foo.index}
{/section}

{section name=bar loop=21 max=6 step=-2}
{$smarty.section.bar.index}
{/section}

// 输出:
10 12 14 16 18

20 18 16 14 12 10

3、section的name 的值是随你定的
Example 7-32. Naming a {section}

{section name=anything loop=$myArray}
{$myArray[anything].foo}
{$name[anything]} //这种用法目前还没怎么用过,也不太清楚
{$address[anything].bar} //这种也是
{/section}

4、遍历一个关联数组,嵌套的数组

$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);
?>

//section 不用嵌套,因为只有一个数组,数组内部用$contacts[customer]得到
//每个数组,再用.键名来得到键值
{section name=customer loop=$contacts}

name: {$contacts[customer].name}

home: {$contacts[customer].home}

cell: {$contacts[customer].cell}

e-mail: {$contacts[customer].email}

{/section}

The above example will output:

name: John Smith

home: 555-555-5555

cell: 666-555-5555

e-mail: john@myexample.com

name: Jack Jones

home phone: 777-555-5555

cell phone: 888-555-5555

e-mail: jack@myexample.com

name: Jane Munson

home phone: 000-555-5555

cell phone: 123456

e-mail: jane@myexample.com

5、从数据库查询记录显示,实际上是显示二维数组,其实同上例一样
$sql = ‘select id, name, home, cell, email from contacts ‘
.”where name like ‘$foo%’ “;
$smarty->assign(‘contacts’, $db->getAll($sql));
?>

// 结果:

{section name=co loop=$contacts} //第一维

//第二维用.号来引用

{sectionelse}

{/section}

Name> Home Cell Email
view {$contacts[co].name} {$contacts[co].home} {$contacts[co].cell} {$contacts[co].email}
No items found

6、嵌套的section

$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);

?>

{section name=customer loop=$custid}

id: {$custid[customer]}

name: {$name[customer]}

address: {$address[customer]}

{section name=contact loop=$contact_type[customer]}
{$contact_type[customer][contact]}: {$contact_info[customer][contact]}

{/section}
{/section}

The above example will output:

id: 1000

name: John Smith

address: 253 N 45th

home phone: 555-555-5555

cell phone: 666-555-5555

e-mail: john@myexample.com

id: 1001

name: Jack Jones

address: 417 Mulberry ln

home phone: 123-456-4

web: www.example.com

id: 1002

name: Jane Munson

address: 5605 apple st

cell phone: 0457878

This entry was posted in 未分类. Bookmark the permalink.

Leave a Reply