




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
smarty学习 foreach Smarty - Manua手册 - Chapter 7. Built-in Functions第7章 内建函数 - foreach,foreachelse用于像访问序数数组一样访问关联数组foreach,foreachelseAttribute Name属性名称Type类型Required必要Default默认值Description描述fromarray数组Yes必要n/aThe array you are looping through循环访问的数组itemstring字符串Yes必要n/aThe name of the variable that is the current element当前元素的变量名keystring字符串No可选n/aThe name of the variable that is the current key当前键名的变量名namestring字符No可选n/aThe name of the foreach loop for accessing foreach properties用于访问foreach属性的foreach循环的名称 from和item是必要属性 foreach循环的name可以是任何字母,数组,下划线的组合,参考PHP变量。 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循环也有自身属性的变量,可以通过$perty访问,其中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 item attribute例 7-5. item属性assign(myArray,$arr);?Template to output $myArray in an un-ordered list用模板以无序列表输出$myArrayforeach from=$myArray item=foo $foo/foreach上例将输出: 1000 1001 1002Example 7-6. Demonstrates the item and key attributes例 7-6. 演示item和key属性Tennis,3=Swimming,8=Coding);$smarty-assign(myArray,$arr);?Template to output $myArray as key/val pair, like PHPs foreach.用模板按键名/键值对的形式输出$myArray, 类似于PHP的foreach。foreach from=$myArray key=k item=v $k: $v/foreachThe above example will output:上例将输出: 9: Tennis 3: Swimming 8: CodingExample 7-7. foreach with associative item attribute例 7-7. foreach的item属性是关联数组array(no=2456,label=Salad),96=array(no=4889,label=Cream);$smarty-assign(items,$items_list);?模板中,url通过$myId输出$itemsforeach from=$items key=myId item=i $i.no: $i.label/foreach上例将输出: 2456: Salad 4889: CreamExample 7-8. foreach with nested item and key例 7-8. foreach使用嵌套的item和keyAssign an array to Smarty, the key contains the key for each looped value.向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 foreach key=key item=item from=$contact $key: $item /foreach/foreach上例将输出: phone: 1 fax: 2 cell: 3 phone: 555-4444 fax: 555-3333 cell: 760-1234Example 7-9. Database example with foreachelse例 7-9. 使用foreachelse的数据库示例一个数据库(例如PEAR或ADODB)的搜索脚本示例,assign(results,$db-getAssoc($sql);?借助foreachelse标记在没有结果时模板输出None found字样。foreach key=cid item=con from=$results $ - $con.nickforeachelse No items were found in the search/foreach.index.index包含当前数组索引,从零开始。Example 7-10. index example例 7-10. index示例*Theheaderblockisoutputeveryfiverows* 每五行输出一次头部区块 *foreachfrom=$itemskey=myIditem=iname=fooif$smarty.foreach.foo.index%5=0Title/if$i.label/foreach.iterationiteration包含当前循环次数,与index不同,从1开始,每次循环增长1。Example 7-11. iteration and index example例 7-11. iteration和index示例*thiswilloutput0|1,1|2,2|3,.etc* 该例将输出0|1, 1|2, 2|3, . 等等 *foreachfrom=$myArrayitem=iname=foo$smarty.foreach.foo.index|$smarty.foreach.foo.iteration,/foreach.firstfirst在当前foreach循环处于初始位置时值为TRUE。Example 7-12. first property example例 7-12. first属性示例*showLATESTonthefirstitem,otherwisetheid* 对于第一个条目显示LATEST而不是id *foreachfrom=$itemskey=myIditem=iname=fooif$smarty.foreach.foo.firstLATESTelse$myId/if$i.label/foreach.lastlast在当前foreach循环处于最终位置是值为TRUE。Example 7-13. last property example例 7-13. last属性示例*Addhorizontalruleatendoflist* 在列表结束时增加一个水平标记 *)foreachfrom=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论