首页 » php

smarty -- foreach用法详解

   发表于:php评论 (1)   热度:2226

 {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
描述fromarrayYes必要n/aThe array you are looping through
循环访问的数组
itemstringYes必要n/aThe name of the variable that is the current element
当前元素的变量名
keystringNo可选n/aThe name of the variable that is the current key
当前键名的变量名
namestringNo可选n/aThe 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 indexiterationfirstlastshowtotal.

{foreach}属性有index, iteration, first, last, show, total.   

Example 7-7. {foreach} with associative item attribute

例 7-7. {foreach}的item属性是关联数组


  $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

 

{foreach from=$items key=myId item=i}

{$i.no}: {$i.label}

{/foreach}

The above example will output:

上例将输出:

2456: Salad

4889: Cream

 

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设置一个数组,对于每个键名对应的每个循环值都包括键。


  $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 key=key item=item from=$contact} {$key}: {$item} {/foreach} {/foreach}

 

The above example will output:

上例将输出:

  phone: 1
fax: 2
cell: 3
phone: 555-4444
fax: 555-3333
cell: 760-1234
 

.index

index contains the current array index, starting with zero.

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

 {* 每五行输出一次头部区块 *}

{foreach from=$items key=myId item=i name=foo}
{if $smarty.foreach.foo.index % 5 == 0}

{/if}

{/foreach}
Title{$i.label}                                                                                                                             

.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 *}

{foreach from=$items key=myId item=i name=foo}




{/foreach}
{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}{$i.label}  

 

.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}
{$prod}{if $smarty.foreach.products.last}{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>
{if $smarty.foreach.foo.last}

{$smarty.foreach.foo.total} items


{/if}
{foreachelse}
... something else ...
{/foreach}                                                                                                                           

See also {section} and $smarty.foreach.

参考{section}和$smarty.foreach。

[Smarty - 官方网站]

http://smarty.php.net/

[Smarty - 下载地址]

当前版本 2.6.18, http://smarty.php.net/do_download.php?download_file=Smarty-2.6.18.tar.gz

全部列表, http://smarty.php.net/download.php

[Smarty - 相关论坛]

http://php.board.newsmth.net/
http://forum.csdn.net/SList/PHP/
http://www.phpinsider.com/smarty-forum/
http://news.php.net/php.smarty.general
http://news.php.net/php.smarty.dev
http://news.php.net/php.smarty.cvs

[Smarty - Manual手册]

借助cycle实现循环输出值, http://smarty.php.net/manual/en/language.function.cycle.php

Smarty的cat函数, http://smarty.php.net/manual/en/language.modifier.cat.php

Smarty的upper函数, http://smarty.php.net/manual/en/language.modifier.upper.php

[Smarty - Manual手册 - Chapter 3. Basic Syntax第3章 基础语法]

Comments注释, http://smarty.php.net/manual/en/language.basic.syntax.php#language.syntax.comments
Math数学运算, http://smarty.php.net/manual/en/language.math.php

 

[Smarty - Manual手册 - Chapter 5. Variable Modifiers第5章 变量修饰符]

cat将值连接到给定变量之后, http://smarty.php.net/manual/en/language.modifier.cat.php

upper将字符串转换成大写字母, http://smarty.php.net/manual/en/language.modifier.upper.php

escape转义修饰符, http://smarty.php.net/manual/en/language.modifier.escape.php

[Smarty - Manual手册 - Chapter 7. Built-in Functions第7章 内建函数]

{foreach},{foreachelse}用于像访问序数数组一样访问关联数组, http://smarty.php.net/manual/en/language.function.foreach.php

 

{strip}在显示之前删除每行前后多余的空格和回车字符, http://smarty.php.net/manual/en/language.function.strip.php

 

[Smarty - Manual手册 - Chapter 8. Custom Functions第8章 自定义函数]

{assign}用于在模板执行过程中设置模板变量, http://smarty.php.net/manual/en/language.custom.functions.php#language.function.assign

{counter}计数器, http://smarty.php.net/manual/en/language.custom.functions.php

[Smarty - Manual手册 - 第13章 Smarty类方法]

register_function()动态注册模板函数插件, http://smarty.php.net/manual/en/api.register.function.php

 

assign_by_ref()按引用赋值, http://smarty.php.net/manual/en/api.assign.by.ref.php

 

(。・v・。)
喜欢这篇文章吗?欢迎分享到你的微博、QQ群,并关注我们的微博,谢谢支持。