smarty -- foreach用法
{foreach},{foreachelse}
用于像访问序数数组一样访问关联数组
{foreach},{foreachelse}
{foreach} is used to loop over an associative array as well a numerically-indexed array, unlike {section} which is for looping over numerically-indexed arrays only. The syntax for {foreach} is much easier than {section}, but as a tradeoff it can only be used for a single array. Every {foreach} tag must be paired with a closing {/foreach} tag.
{foreach} 用于像循环访问一个数字索引数组一样循环访问一个关联数组,与仅能访问数字索引数组的{section}不同,{foreach}的语法比 {section}的语法简单得多,但是作为一个折衷方案也仅能用于单个数组。每个{foreach}标记必须与关闭标记{/foreach}成对出现。
Attribute Name 属性名称 |
Type 类型 |
Required 必要 |
Default 默认值 |
Description 描述 |
---|---|---|---|---|
from | array | Yes必要 | n/a | The array you are looping through 循环访问的数组 |
item | string | Yes必要 | n/a | The name of the variable that is the current element 当前元素的变量名 |
key | string | No可选 | n/a | The name of the variable that is the current key 当前键名的变量名 |
name | string | No可选 | n/a | The name of the foreach loop for accessing foreach properties 用于访问foreach属性的foreach循环的名称 |
Required attributes are from and item.
- from和item是必要属性
The name of the {foreach} loop can be anything you like, made up of letters, numbers and underscores, like PHP variables.
- {foreach}循环的name可以是任何字母,数组,下划线的组合,参考PHP变量。
{foreach} loops can be nested, and the nested {foreach} names must be unique from each other.
- {foreach}循环可以嵌套,嵌套的{foreach}的名称应当互不相同。
The from attribute, usually an array of values, determines the number of times {foreach} will loop.
- from属性通常是值数组,被用于判断{foreach}的循环次数。
{foreachelse} is executed when there are no values in the from variable.
- 在from变量中没有值时,将执行{foreachelse}。
{foreach} loops also have their own variables that handle properties. These are accessed with: {$smarty.foreach.name.property} with "name" being the nameattribute.
{foreach}循环也有自身属性的变量,可以通过{$smarty.foreach.name.property}访问,其中"name"是name属性。
Note: The name attribute is only required when you want to access a {foreach} property, unlike {section}. Accessing a {foreach} property with nameundefined does not throw an error, but leads to unpredictable results instead.
注意:name属性仅在需要访问{foreach}属性时有效,与{section}不同。访问未定义name的{foreach}属性不会抛出一个错误,但将导致不可预知的结果。
{foreach} properties are index, iteration, first, last, show, total.
- {foreach}属性有index, iteration, first, last, show, total.
- Example 7-7. {foreach} with associative item 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);
- ?>
- Template to output $items with $myId in the url
- 模板中,url通过$myId输出$items
- <ul>
- {foreach from=$items key=myId item=i}
- <li><a href="http://blog.163.com/lgh_2002/blog/item.php?id={$myId}">{$i.no}: {$i.label}</li>
- {/foreach}
- </ul>
- The above example will output:
- 上例将输出:
- <ul>
- <li><a href="http://blog.163.com/lgh_2002/blog/item.php?id=23">2456: Salad</li>
- <li><a href="http://blog.163.com/lgh_2002/blog/item.php?id=96">4889: Cream</li>
- </ul>
- Example 7-8. {foreach} with nested item and key
- 例 7-8. {foreach}使用嵌套的item和key
- Assign an array to Smarty, the key contains the key for each looped 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}
- The above example will output:
- 上例将输出:
- <hr />
- phone: 1<br />
- fax: 2<br />
- cell: 3<br />
- <hr />
- phone: 555-4444<br />
- fax: 555-3333<br />
- cell: 760-1234<br />
.index
index contains the current array index, starting with zero.
.index包含当前数组索引,从零开始。
- {* 每五行输出一次头部区块 *}
- <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 contains the current loop iteration and always starts at one, unlike index. It is incremented by one on each iteration.
iteration包含当前循环次数,与index不同,从1开始,每次循环增长1。
- {* 该例将输出0|1, 1|2, 2|3, ... 等等 *}
- {foreach from=$myArray item=i name=foo}
- {$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
- {/foreach}
.first
first is TRUE if the current {foreach} iteration is the initial one.
first在当前{foreach}循环处于初始位置时值为TRUE。
- {* 对于第一个条目显示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 is set to TRUE if the current {foreach} iteration is the final one.
last在当前{foreach}循环处于最终位置是值为TRUE。
- {* 在列表结束时增加一个水平标记 *})
- {foreach from=$items key=part_id item=prod name=products}
- <a href="http://blog.163.com/lgh_2002/blog/#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if}
- {foreachelse}
- ... content ...
- {/foreach}
.show
show is used as a parameter to {foreach}. show is a boolean value. If FALSE, the {foreach} will not be displayed. If there is a {foreachelse} present, that will be alternately displayed.
show是{foreach}的参数. show是一个布尔值。如果值为FALSE,{foreach}将不被显示。如果有对应的{foreachelse},将被显示。
.total
total contains the number of iterations that this {foreach} will loop. This can be used inside or after the {foreach}.
total包括{foreach}将循环的次数,既可以在{foreach}中使用,也可以在之后使用。
- {* 在结束位置显示行数 *}
- {foreach from=$items key=part_id item=prod name=foo}
- {$prod.name><hr/>
- {if $smarty.foreach.foo.last}
- <div id="total">{$smarty.foreach.foo.total} items</div>
- {/if}
- {foreachelse}
- ... something else ...
- {/foreach}
See also {section} and $smarty.foreach.
smarty -- foreach用法的更多相关文章
- smarty -- foreach用法详解
{foreach},{foreachelse} 用于像访问序数数组一样访问关联数组 {foreach},{foreachelse} {foreach} is used to loop over an ...
- smarty foreach 最全用法
<?php$search_condition = "where name like '$foo%' ";$sql = 'select contact_id, name, ni ...
- Smarty基础用法
一.Smarty基础用法: 1.基础用法如下 include './smarty/Smarty.class.php';//引入smarty类 $smarty = new Smarty();//实例化s ...
- smarty foreach循环
1,smarty foreach1,单纯的数组array(1000,2000,3000),使用foreach(from = $array item=foo){$foo}2,键值对数组<ul> ...
- *ecshop 模板中foreach用法详解
1.foreach分以下几个参数 from, item, name, iteration, index 2.使用foreach循环 如果php要传递一个数组(如:$array)给ecshop ...
- (转)Smarty Foreach 使用说明
foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案). foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是 ...
- PHP中foreach()用法汇总
这篇文章主要给大家详细介绍了PHP中foreach()用法以及相关的示例,十分的细致,有需要的小伙伴可以参考下. PHP 4 引入了 foreach 结构,和 Perl 以及其他语言很像.这只是一种遍 ...
- jQuery each和js forEach用法比较
本文实例分析了jQuery each和js forEach用法.分享给大家供大家参考,具体如下: 对于遍历数组的元素,js代码和jquery都有类似的方法,js用的是forEach而jquery用的是 ...
- 【JS】<c:foreach>用法
<c:foreach>类似于for和foreach循环 以下是我目前见过的用法: 1.循环遍历,输出所有的元素. <c:foreach items="${list}&q ...
随机推荐
- 为什么数值类型byte取值范围是(-128~127)?
在解决这个问题之前,我们先了解几个概念? 一.原码, 反码, 补码的概念 正数的反码和补码都与原码一样: 负数的反码.补码与原码不同,负数的反码:原码中除去符号位,其他的数值位取反,0变1,1变0.负 ...
- 02/07/2106 @ 6:28am (UTC)
<?php echo pow(2,32); 4294967296 http://www.unixtimestamp.com/index.php 4294967296 Is equivalent ...
- data-"mit.edu-Thinking In C++"
Volume 2 ctrl+s http://web.mit.edu/merolish/ticpp/TicV2.html http://web.mit.edu/merolish/ticpp/TicV2 ...
- 4Web镇之旅:开始链接
为了将网页发布到web上,需要一个全日工作的网络服务器,最好的方法是找到一家主机代理商. 域名是用来定位网站的第一无二的名字. 网页的最顶层目录就是根目录.在Web服务器中,因为根目录中的东西有可能在 ...
- 制作3D图片立方体旋转特效
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>CS ...
- Bluetooth LMP介绍
目录 1. 介绍 2. 数据包格式(Packet Format) 3. Procedure Rules 4. 通用回应消息(General Response Messages) 5. 设备特性(Dev ...
- MySQL- 锁(2)
InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点意味着:只有 ...
- python之列表切片(slice)
使用索引获取列表的元素(随机读取) 列表元素支持用索引访问,正向索引从0开始 colors=["red","blue","green"] c ...
- 【Android测试】【第十节】MonkeyRunner—— 录制回放
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4861693.html 前言 在实际项目进行过程中,频繁的需 ...
- Marriage Match IV---hdu3416(spfa + Dinic)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3416 有一个有向图,n个点,m条边,给一个起点和终点,求出从起点到终点的最短路共有几条,每 ...