Smarty中section的用法

1、循环一个简单的一维数组:

<?php

$data = array(1000,1001,1002);

$smarty->assign(‘custid‘,$data);

?>

//customer和下面的foo可以随便命名,作用其实仅仅是一个index下标,用来引用数组中的元素

{section name=customer loop=$custid}

id: {$custid[customer]}<br />

{/section}

<hr />

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

{$custid[foo]}<br />

{/section}

//输出

id: 1000<br />

id: 1001<br />

id: 1002<br />

<hr />

id: 1002<br />

id: 1001<br />

id: 1000<br />

2、不用assign数组直接在smarty中循环:

//特别地设置了start,step属性用来控制循环

//$smarty.section.section的名字.index是一个特殊变量,用来显示当前循环的位置

{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

3、section的name的值是随你定的

Example 7-32. Naming a {section}

{section name=anything loop=$myArray}

{$myArray[anything].foo}

{$name[anything]}     //这种用法目前还没怎么用过,也不太清楚

{$address[anything].bar}    //这种也是

{/section}

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

<?php

$data = array(

array(‘name‘ => ‘John Smith‘, ‘home‘ => ‘555-555-5555‘,

‘cell‘ => ‘666-555-5555‘, ‘email‘ => ‘[email protected]‘),

array(‘name‘ => ‘Jack Jones‘, ‘home‘ => ‘777-555-5555‘,

‘cell‘ => ‘888-555-5555‘, ‘email‘ => ‘[email protected]‘),

array(‘name‘ => ‘Jane Munson‘, ‘home‘ => ‘000-555-5555‘,

‘cell‘ => ‘123456‘, ‘email‘ => ‘[email protected]‘)

);

$smarty->assign(‘contacts‘,$data);

?>

//section不用嵌套,因为只有一个数组,数组内部用$contacts[customer]得到

//每个数组,再用.键名来得到键值

{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}

The above example will output:

<p>

name: John Smith<br />

home: 555-555-5555<br />

cell: 666-555-5555<br />

e-mail: [email protected]

</p>

<p>

name: Jack Jones<br />

home phone: 777-555-5555<br />

cell phone: 888-555-5555<br />

e-mail: [email protected]

</p>

<p>

name: Jane Munson<br />

home phone: 000-555-5555<br />

cell phone: 123456<br />

e-mail: [email protected]

</p>

5、从数据库查询记录显示,实际上是显示二维数组,其实同上例一样

<?php

$sql = ‘select id, name, home, cell, email from contacts ‘

."where name like ‘$foo%‘ ";

$smarty->assign(‘contacts‘, $db->getAll($sql));

?>

//结果:

<table>

<tr><th>&nbsp;</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>

6、嵌套的section

<?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‘, ‘[email protected]‘),

array( ‘123-456-4‘, ‘www.example.com‘),

array( ‘0457878‘)

);

$smarty->assign(‘contact_info‘, $info);

?>

{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}

The above example will output:

<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: [email protected]<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 />

时间: 2024-08-04 21:48:03

Smarty中section的用法的相关文章

Smarty中section的使用

在smarty的使用过程中,有很多时候需要将一个数组输出到模板中来处理,以下将演示如何将一个索引(index)数组和关联(assocaite)数组在页面中展现出来. 本文中假设有如下一个索引数组 1.索引数组 1     $people = array('tony','sweety','abc','four');2     $smarty->assign('people',$people); 在模板中显示: 1 {section name=n loop=$people}2     name:{$

smarty模板section循环用法

{foreach name=foreach_name key=k item=v from=$arr} {$k}=>{$v}<br/> {/foreach} {section name=sec_name loop=$arr start=0 step=n max= m show=true/false } {if is_array($arr[sec_name])} {section name=sec2 loop=$arr[sec_name]} {$arr[sec_name][sec2]}<

smarty 中时间格式化的用法

大家都知道PHP中输出时间和日期可以用 date("Y-m-d H:i:s",时间戳)  , 但是在smarty模板中,$time|date_format:'%Y-%m-%d %H:%M:%S' , 这个让我找了很久.原来 在smarty中,分钟 不是用i ,使用M .留着做记号.

Smarty中一些标签的使用

Smarty中的标签和php中的标签不一样 foreach标签{foreach   from=$goods(变量名) key='键,不带$' item='值,不带$'}中间的显示内容{/foreach} section标签{section loop=$goods变量名 name=临时角标} 显示内容$goods[临时角标].goods_id{/section} while循环标签{while $age<18}中间是显示内容,$age要进行运算,否则将是死循环{/while} 封装类mySmart

IOS中TableView的用法

IOS中TableView的用法 一.UITableView 1.数据展示的条件 1> UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITableView的dataSource数据源对象 2> 要想当UITableView的dataSource对象,必须遵守UITableViewDataSource协议,实现相应的数据源方法 3> 当UITableView想要展示数据的时候,就会给数据源发送消息(调用数据源

[转]c++ 中__declspec 的用法

c++ 中__declspec 的用法 转载地址:http://www.cnblogs.com/ylhome/archive/2010/07/10/1774770.html c++ 中__declspec 的用法 语法说明: __declspec ( extended-decl-modifier-seq ) 扩展修饰符: 1:align(#) 用__declspec(align(#))精确控制用户自定数据的对齐方式 ,#是对齐值. e.g __declspec(align(32)) struct

二、Smarty中的三种主要变量

1.从PHP中分配的变量 $smarty -> assign(); 从PHP分配给模板使用的变量:动态变量 2.从配置文件中读取的变量 $smarty配置文件中的内容不是PHP读取,而是就在smarty模板中应用变量,让用户修改模板的板式.外观. 1)  配置文件需要放置在什么位置,需要设置多少个配置文件,及如何去命名 2)  配置文件该如何编写 3)  如何在模板中找到配置文件? 4)  如何在模板中读取配置文件中的内容? 例:创建配置文件show.conf,内容为: Bodycolor=bl

critical section的用法

critical section Critical Section: 不论是硬件临界资源,还是软件临界资源,多个进程必须互斥地对它进行访问.每个进程中访问临界资源的那段代码称为临界区(Critical Section). 每个进程中访问临界资源的那段程序称为临界区(Critical Section)(临界资源是一次仅允许一个进程使用的共享资源).每次只准许一个进程进入临界区,进入后不允许其他进程进入.不论是硬件临界资源,还是软件临界资源,多个进程必须互斥地对它进行访问. 多个进程中涉及到同一个临

Oracle 中 decode 函数用法

Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译值1)ELSIF 条件=值2 THEN RETURN(翻译值2) ......ELSIF 条件=值n THEN RETURN(翻译值n)ELSE RETURN(缺省值)END IFdecode(字段或字段的运算,值1,值2,值3) 这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值