smarty學(xué)習(xí)foreach.doc_第1頁(yè)
smarty學(xué)習(xí)foreach.doc_第2頁(yè)
smarty學(xué)習(xí)foreach.doc_第3頁(yè)
smarty學(xué)習(xí)foreach.doc_第4頁(yè)
smarty學(xué)習(xí)foreach.doc_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

smarty學(xué)習(xí) foreach Smarty - Manua手冊(cè) - Chapter 7. Built-in Functions第7章 內(nèi)建函數(shù) - foreach,foreachelse用于像訪問序數(shù)數(shù)組一樣訪問關(guān)聯(lián)數(shù)組foreach,foreachelseAttribute Name屬性名稱Type類型Required必要Default默認(rèn)值Description描述fromarray數(shù)組Yes必要n/aThe array you are looping through循環(huán)訪問的數(shù)組itemstring字符串Yes必要n/aThe name of the variable that is the current element當(dāng)前元素的變量名keystring字符串No可選n/aThe name of the variable that is the current key當(dāng)前鍵名的變量名namestring字符No可選n/aThe name of the foreach loop for accessing foreach properties用于訪問foreach屬性的foreach循環(huán)的名稱 from和item是必要屬性 foreach循環(huán)的name可以是任何字母,數(shù)組,下劃線的組合,參考PHP變量。 foreach循環(huán)可以嵌套,嵌套的foreach的名稱應(yīng)當(dāng)互不相同。 The from attribute, usually an array of values, determines the number of times foreach will loop. from屬性通常是值數(shù)組,被用于判斷foreach的循環(huán)次數(shù)。 foreachelse is executed when there are no values in the from variable. 在from變量中沒有值時(shí),將執(zhí)行foreachelse。 foreach循環(huán)也有自身屬性的變量,可以通過$perty訪問,其中name是name屬性。注意:name屬性僅在需要訪問foreach屬性時(shí)有效,與section不同。訪問未定義name的foreach屬性不會(huì)拋出一個(gè)錯(cuò)誤,但將導(dǎo)致不可預(yù)知的結(jié)果。 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用模板以無(wú)序列表輸出$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.用模板按鍵名/鍵值對(duì)的形式輸出$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屬性是關(guān)聯(lián)數(shù)組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設(shè)置一個(gè)數(shù)組,對(duì)于每個(gè)鍵名對(duì)應(yīng)的每個(gè)循環(huán)值都包括鍵。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的數(shù)據(jù)庫(kù)示例一個(gè)數(shù)據(jù)庫(kù)(例如PEAR或ADODB)的搜索腳本示例,assign(results,$db-getAssoc($sql);?借助foreachelse標(biāo)記在沒有結(jié)果時(shí)模板輸出None found字樣。foreach key=cid item=con from=$results $ - $con.nickforeachelse No items were found in the search/foreach.index.index包含當(dāng)前數(shù)組索引,從零開始。Example 7-10. index example例 7-10. index示例*Theheaderblockisoutputeveryfiverows* 每五行輸出一次頭部區(qū)塊 *foreachfrom=$itemskey=myIditem=iname=fooif$smarty.foreach.foo.index%5=0Title/if$i.label/foreach.iterationiteration包含當(dāng)前循環(huán)次數(shù),與index不同,從1開始,每次循環(huán)增長(zhǎng)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在當(dāng)前foreach循環(huán)處于初始位置時(shí)值為TRUE。Example 7-12. first property example例 7-12. first屬性示例*showLATESTonthefirstitem,otherwisetheid* 對(duì)于第一個(gè)條目顯示LATEST而不是id *foreachfrom=$itemskey=myIditem=iname=fooif$smarty.foreach.foo.firstLATESTelse$myId/if$i.label/foreach.lastlast在當(dāng)前foreach循環(huán)處于最終位置是值為TRUE。Example 7-13. last property example例 7-13. last屬性示例*Addhorizontalruleatendoflist* 在列表結(jié)束時(shí)增加一個(gè)水平標(biāo)記 *)foreachfrom=

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論