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 withname undefined 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属性是关联数组
Template to output $items with $myId in the url 模板中,url通过$myId输出$items
The above example will output: 上例将输出:
|
|
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设置一个数组,对于每个键名对应的每个循环值都包括键。
The template to output $contact. 用于输出$contact的模板。
The above example will output: 上例将输出:
|
.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} |
smarty -- foreach用法详解的更多相关文章
- *ecshop 模板中foreach用法详解
1.foreach分以下几个参数 from, item, name, iteration, index 2.使用foreach循环 如果php要传递一个数组(如:$array)给ecshop ...
- js数组中foEach和map的用法详解 jq中的$.each和$.map
数组中foEach和map的用法详解 相同点: 1.都是循环遍历数组(仅仅是数组)中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项value, ...
- 2020了你还不会Java8新特性?(五)收集器比较器用法详解及源码剖析
收集器用法详解与多级分组和分区 为什么在collectors类中定义一个静态内部类? static class CollectorImpl<T, A, R> implements Coll ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...
- MyBatis的Mapper文件的foreach标签详解
MyBatis的Mapper文件的foreach标签用来迭代用户传递过来的Lise或者Array,让后根据迭代来拼凑或者批量处理数据.如:使用foreach来拼接in子语句. 在学习MyBatis M ...
- linux管道命令grep命令参数及用法详解---附使用案例|grep
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
- mysql中event的用法详解
一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...
- CSS中伪类及伪元素用法详解
CSS中伪类及伪元素用法详解 伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...
随机推荐
- 微信支付、支付宝支付和QQ钱包支付
最近忙于对接微信支付和支付宝支付,注册微信公众号,认证公众号,注册微信支付商户号并进行认证: 签约支付宝支付产品(手机网站支付.PC网站支付),注册支付宝企业账号(企业账号权限更大): 注册QQ钱包商 ...
- 洛谷P1288 取数游戏II 题解 博弈论
题目链接:https://www.luogu.org/problem/P1288 首先,如果你的一边的边是 \(0\) ,那么你肯定走另一边. 那么你走另一边绝对不能让这条边有剩余,因为这条边有剩余的 ...
- npx cowsay 让动物说话~
发现个好玩的东东, 忍不住想分享出来, 好可爱, 哈哈哈~~ node环境执行命令: npm i cowsay -D npx cowsay hello! npx cowsay -f sh ...
- Tensorflow安装问题: Could not find a version that satisfies the requirement tensorflow pip命令
引言: Tensorflow大名鼎鼎,这里不再赘述其为何物.这里讲描述在安装python包的时候碰到的“No matching distribution found for tensorflow”,其 ...
- 关于IFRAME的onload事件
昨天遇到一个关于iframe的问题,比如a页面中嵌入了一个iframe称为a_iframe,如果直接在a_iframe的标签上直接加入属性的设置,onload=’’,这样才onload事件才是起作用的 ...
- C# 在 构造函数添加 CallerMemberName 会怎样
在 C# 中有一个特性 CallerMemberName 可以给方法知道调用这个方法的方法名,在 UWP 中用这个特性很多,特别是在使用 MVVM 绑定 如果在构造函数使用这个特性会发生什么? 构造函 ...
- Git的使用--如何将本地项目上传到Github(两种简单、方便的方法..)
https://blog.csdn.net/u014135752/article/details/79951802 总结:其实只需要进行下面几步就能把本地项目上传到Github 1.在本地创建一个版本 ...
- MySQL排序问题
直接上SQL语句: SELECT * FROM user_test ORDER BY user_name ,user_money DESC; #user_name中如果是数字会先排在前面,然后在按字母 ...
- Mockito -- 入门篇
Mockito是一种mock工具/框架.我理解EasyMock有点过时了,Mockito是现在比较流行的. 什么是mock?说的直白一点,大家都知道unit test应该是尽可能独立的.对一个clas ...
- Python的驻留机制(仅对数字,字母,下划线有效)
Python的驻留机制及为在同一运行空间内,当两变量的值相同,则地址也相同. 举例: a = 'abc' b = 'abc' print(id(a)) print(id(b)) 以上示例为驻留机制有效 ...