smarty --foreach详解
来源:互联网 发布:校园网因为网卡mac地址 编辑:程序博客网 时间:2024/05/16 11:05
循环访问的数组itemstring字符串Yes必要n/aThe name of the variable that isthe current element
当前元素的变量名keystring字符串No可选n/aThe name of the variable that isthe current key
当前键名的变量名namestring字符No可选n/aThe name of the foreach loop foraccessing foreach properties
用于访问foreach属性的foreach循环的名称
- from和item是必要属性
- {foreach}循环的name可以是任何字母,数组,下划线的组合,参考PHP变量。
- {foreach}循环可以嵌套,嵌套的{foreach}的名称应当互不相同。
The from attribute, usually an array ofvalues, determines the number of times{foreach} willloop.
- from属性通常是值数组,被用于判断{foreach}的循环次数。
{foreachelse} is executed when there are no values inthe from variable.
- 在from变量中没有值时,将执行{foreachelse}。
{foreach}循环也有自身属性的变量,可以通过{$smarty.foreach.name.property}访问,其中"name"是name属性。
注意:name属性仅在需要访问{foreach}属性时有效,与{section}不同。访问未定义name的{foreach}属性不会抛出一个错误,但将导致不可预知的结果。
{foreach} properties are index, iteration, first, last, show, total.
- {foreach}属性有index, iteration, first, last, show,total.
Example 7-5. The itemattribute
例 7-5. item属性
<?php
$arr = array( 1000, 1001, 1002);
$smarty->assign('myArray', $arr);
?>
Template to output $myArray in an un-orderedlist
用模板以无序列表输出$myArray
<ul>{foreach from=$myArray item=foo} <li>{$foo}</li>{/foreach}</ul>
上例将输出:
<ul> <li>1000</li> <li>1001</li> <li>1002</li></ul>
Example 7-6. Demonstrates the item andkey attributes
例 7-6. 演示item和key属性
<?php
$arr = array( 9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding');
$smarty->assign('myArray', $arr);
?>
Template to output $myArray as key/val pair,like PHP's foreach.
用模板按键名/键值对的形式输出$myArray, 类似于PHP的foreach。
<ul>{foreach from=$myArray key=k item=v} <li>{$k}: {$v}</li>{/foreach}</ul>
The above example will output:
上例将输出:
<ul> <li>9: Tennis</li> <li>3: Swimming</li> <li>8: Coding</li></ul>
Example 7-7. {foreach} with associativeitem attribute
例 7-7. {foreach}的item属性是关联数组
<?php
$items_list = array( 23 => array( 'no' => 2456, 'label' => 'Salad'),
96 => array( 'no' => 4889, 'label' => 'Cream')
);
$smarty->assign('items', $items_list);
?>
模板中,url通过$myId输出$items
<ul>{foreach from=$items key=myId item=i} <li><a href="item.php?id={$myId}">{$i.no}: {$i.label}</li>{/foreach}</ul>
上例将输出:
<ul> <li><a href="item.php?id=23">2456: Salad</li> <li><a href="item.php?id=96">4889: Cream</li></ul>
Example 7-8. {foreach} with nesteditem and key
例 7-8.{foreach}使用嵌套的item和key
Assign an array to Smarty, the key contains the key for eachlooped value.
向Smarty设置一个数组,对于每个键名对应的每个循环值都包括键。
<?php
$smarty ->assign('contacts', array(
array( 'phone' => '1',
'fax' => '2',
'cell' => '3'),
array( 'phone' => '555-4444',
'fax' => '555-3333',
'cell' => '760-1234')
));
?>
The template to output $contact.
用于输出$contact的模板。
{foreach name=outer item=contact from=$contacts} <hr /> {foreach key=key item=item from=$contact} {$key}: {$item}<br /> {/foreach}{/foreach}
上例将输出:
<hr /> phone: 1<br /> fax: 2<br /> cell: 3<br /><hr /> phone: 555-4444<br /> fax: 555-3333<br /> cell: 760-1234<br />
Example 7-9. Database example with{foreachelse}
例 7-9. 使用{foreachelse}的数据库示例
一个数据库(例如PEAR或ADODB)的搜索脚本示例,
<?php
$search_condition = "where name like '$foo%' " ;
$sql = 'select contact_id, name, nick from contacts ' .$search_condition.' order by name' ;
$smarty->assign('results', $db->getAssoc($sql) );
?>
借助{foreachelse}标记在没有结果时模板输出"None found"字样。
{foreach key=cid item=con from=$results} <a href="contact.php?contact_id={$cid}">{$con.name} - {$con.nick}</a><br />{foreachelse} No items were found in the search{/foreach}
.index
.index包含当前数组索引,从零开始。
Example 7-10. indexexample
例 7-10. index示例
{* The header block is output every five rows *}
{* 每五行输出一次头部区块 *}
<table>
{foreach from=$items key=myId item=i name=foo}
{if $smarty.foreach.foo.index % 5 == 0}
<tr><th>Title</th></tr>
{/if}
<tr><td>{$i.label}</td></tr>
{/foreach}
</table>
.iteration
iteration包含当前循环次数,与index不同,从1开始,每次循环增长1。
Example 7-11. iteration andindex example
例 7-11. iteration和index示例
{* this will output 0|1, 1|2, 2|3, ... etc *}
{* 该例将输出0|1, 1|2, 2|3, ... 等等 *}
{foreach from=$myArray item=i name=foo}
{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
{/foreach}
.first
first在当前{foreach}循环处于初始位置时值为TRUE。
Example 7-12. first propertyexample
例 7-12. first属性示例
{* show LATEST on the first item, otherwise the id *}
{* 对于第一个条目显示LATEST而不是id *}
<table>
{foreach from=$items key=myId item=i name=foo}
<tr>
<td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td>
<td>{$i.label}</td>
</tr>
{/foreach}
</table>
.last
last在当前{foreach}循环处于最终位置是值为TRUE。
Example 7-13. last propertyexample
例 7-13. last属性示例
{* Add horizontal rule at end of list *}
{* 在列表结束时增加一个水平标记 *})
{foreach from=$items key=part_id item=prod name=products}
<a href="#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if}
{foreachelse}
... content ...
{/foreach}
.show
- smarty --foreach详解
- smarty --foreach详解
- smarty --foreach详解
- smarty --foreach详解
- ecshop模板smarty foreach详解
- smarty的foreach使用详解
- ecshop模板smarty foreach详解
- ecshop模板smarty foreach详解
- ecshop模板smarty foreach详解
- ecshop模板的smarty foreach详解
- ecshop模板smarty foreach详解 [ECshop]
- smarty中section与foreach详解
- ECShop模板中smarty foreach标签详解
- 【转载】smarty --foreach详解(不错不错)
- smarty循环foreach,section实例详解
- smarty手册-smarty中foreach循环语句详解
- smarty foreach
- Smarty{foreach}
- WebGL自学教程——WebGL示例:14.0 代码整理
- java中常用的字符串的截取方法
- Java中重载和重写的区别
- php中selt和this 的区别
- ASP.NET设置Session过期时间
- smarty --foreach详解
- 字符串转换成JSON的三种方式
- 深入理解C语言
- get post 表单提交 NameValueCollection 兼容两种提交方式
- ubuntu tftp服务器
- 自己学习到MFC的知识
- windows linux双系统删除linux步骤
- Ext 优缺点
- 理解ThreadLocal