{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 indexiterationfirstlastshowtotal.

  • {foreach}属性有index, iteration, first, last, show, total.
  1. Example 7-7. {foreach} with associative item attribute
  2. 例 7-7. {foreach}的item属性是关联数组
  3. <?php
  4. $items_list = array(
  5. 23 => array('no' => 2456, 'label' => 'Salad'),
  6. 96 => array('no' => 4889, 'label' => 'Cream')
  7. );
  8. $smarty->assign('items', $items_list);
  9. ?>
  10. Template to output $items with $myId in the url
  11. 模板中,url通过$myId输出$items
  12. <ul>
  13. {foreach from=$items key=myId item=i}
  14. <li><a href="http://blog.163.com/lgh_2002/blog/item.php?id={$myId}">{$i.no}: {$i.label}</li>
  15. {/foreach}
  16. </ul>
  17. The above example will output:
  18. 上例将输出:
  19. <ul>
  20. <li><a href="http://blog.163.com/lgh_2002/blog/item.php?id=23">2456: Salad</li>
  21. <li><a href="http://blog.163.com/lgh_2002/blog/item.php?id=96">4889: Cream</li>
  22. </ul>
  1. Example 7-8. {foreach} with nested item and key
  2. 例 7-8. {foreach}使用嵌套的item和key
  3. Assign an array to Smarty, the key contains the key for each looped value.
  4. 向Smarty设置一个数组,对于每个键名对应的每个循环值都包括键。
  5. <?php
  6. $smarty->assign('contacts', array(
  7. array('phone' => '1',
  8. 'fax' => '2',
  9. 'cell' => '3'),
  10. array('phone' => '555-4444',
  11. 'fax' => '555-3333',
  12. 'cell' => '760-1234')
  13. ));
  14. ?>
  15. The template to output $contact.
  16. 用于输出$contact的模板。
  17. <foreach name=outer item=contact from=$contacts}
  18. <hr />
  19. {foreach key=key item=item from=$contact}
  20. {$key}: {$item}<br />
  21. {/foreach}
  22. {/foreach}
  23. The above example will output:
  24. 上例将输出:
  25. <hr />
  26. phone: 1<br />
  27. fax: 2<br />
  28. cell: 3<br />
  29. <hr />
  30. phone: 555-4444<br />
  31. fax: 555-3333<br />
  32. cell: 760-1234<br />

.index

index contains the current array index, starting with zero.

.index包含当前数组索引,从零开始。

  1. {* 每五行输出一次头部区块 *}
  2. <table>
  3. {foreach from=$items key=myId item=i name=foo}
  4. {if $smarty.foreach.foo.index % 5 == 0}
  5. <tr><th>Title</th></tr>
  6. {/if}
  7. <tr><td>{$i.label}</td></tr>
  8. {/foreach}
  9. </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。

  1. {* 该例将输出0|1, 1|2, 2|3, ... 等等 *}
  2. {foreach from=$myArray item=i name=foo}
  3. {$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
  4. {/foreach}

.first

first is TRUE if the current {foreach} iteration is the initial one.

first在当前{foreach}循环处于初始位置时值为TRUE。

  1. {* 对于第一个条目显示LATEST而不是id *}
  2. <table>
  3. {foreach from=$items key=myId item=i name=foo}
  4. <tr>
  5. <td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td>
  6. <td>{$i.label}</td>
  7. </tr>
  8. {/foreach}
  9. </table>

.last

last is set to TRUE if the current {foreach} iteration is the final one.

last在当前{foreach}循环处于最终位置是值为TRUE。

  1. {* 在列表结束时增加一个水平标记 *})
  2. {foreach from=$items key=part_id item=prod name=products}
  3. <a href="http://blog.163.com/lgh_2002/blog/#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if}
  4. {foreachelse}
  5. ... content ...
  6. {/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}中使用,也可以在之后使用。

  1. {* 在结束位置显示行数 *}
  2. {foreach from=$items key=part_id item=prod name=foo}
  3. {$prod.name><hr/>
  4. {if $smarty.foreach.foo.last}
  5. <div id="total">{$smarty.foreach.foo.total} items</div>
  6. {/if}
  7. {foreachelse}
  8. ... something else ...
  9. {/foreach}

See also {section} and $smarty.foreach.

smarty -- foreach用法的更多相关文章

  1. smarty -- foreach用法详解

    {foreach},{foreachelse} 用于像访问序数数组一样访问关联数组 {foreach},{foreachelse} {foreach} is used to loop over an  ...

  2. smarty foreach 最全用法

    <?php$search_condition = "where name like '$foo%' ";$sql = 'select contact_id, name, ni ...

  3. Smarty基础用法

    一.Smarty基础用法: 1.基础用法如下 include './smarty/Smarty.class.php';//引入smarty类 $smarty = new Smarty();//实例化s ...

  4. smarty foreach循环

    1,smarty foreach1,单纯的数组array(1000,2000,3000),使用foreach(from = $array item=foo){$foo}2,键值对数组<ul> ...

  5. *ecshop 模板中foreach用法详解

    1.foreach分以下几个参数 from, item, name, iteration, index 2.使用foreach循环      如果php要传递一个数组(如:$array)给ecshop ...

  6. (转)Smarty Foreach 使用说明

    foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案). foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是 ...

  7. PHP中foreach()用法汇总

    这篇文章主要给大家详细介绍了PHP中foreach()用法以及相关的示例,十分的细致,有需要的小伙伴可以参考下. PHP 4 引入了 foreach 结构,和 Perl 以及其他语言很像.这只是一种遍 ...

  8. jQuery each和js forEach用法比较

    本文实例分析了jQuery each和js forEach用法.分享给大家供大家参考,具体如下: 对于遍历数组的元素,js代码和jquery都有类似的方法,js用的是forEach而jquery用的是 ...

  9. 【JS】<c:foreach>用法

    <c:foreach>类似于for和foreach循环   以下是我目前见过的用法: 1.循环遍历,输出所有的元素. <c:foreach items="${list}&q ...

随机推荐

  1. phaser

    phaser http://www.csdn.net/article/2015-10-08/2825865 https://github.com/gavinanderegg/coffeeQuest

  2. 设置MyEclipse开发项目时使用的JDK

    安装好MyEclipse之后,在MyEclipse中开发项目时,默认使用的是MyEclipse是自带的JDK,如下图所示: 如果我们需要使用自己安装好的JDK,那么就需要在MyEclipse中重新设置 ...

  3. Linux core 文件介绍

    Linux core 文件介绍 http://www.cnblogs.com/dongzhiquan/archive/2012/01/20/2328355.html 1. core文件的简单介绍在一个 ...

  4. .Net搭建的WebService测试页使用TextArea大文本框方便调试

    用.Net搭建的WebService,系统默认提供了测试页,供大家输入参数进行测试.但因为参数输入框使用的是单行input控件,导致无法输入换行文本,使得有些参数(如换行的xml)无法输入,及其不便. ...

  5. C#远程共享文件路径访问

    public class Win32ServiceManager    {        private string strPath;        private ManagementClass ...

  6. ActiveMQ发布订阅模式

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  7. MVC从服务器端返回js到客户端的方法(总结)

    1.利用ViewBag,从服务器端创建一个显示js开关的ViewBag,然后到View中去做判断. Controller端 [HttpPost] public ActionResult Index(h ...

  8. Partial Tree---hdu5534(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意:有n个节点,让这n个节点形成一棵树,这棵树的种类有很多种,现在告诉n-1个f[i],表示度 ...

  9. Sparse Graph---hdu5876(set+bfs+补图求最短路)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5876 题意:有一个含有n个点的无向图,已知图的补图含有m条边u, v:求在原图中,起点s到 ...

  10. postgresql 将查询结果导出到文件

    方法1:进入查询终端,输入\o  aa.out 查询结果将输出到当前目录的aa.out 文件 方法2: 将查询语句写a.sql中, alias sql2="export PGPASSWORD ...