smarty foreach詳解

來源:互聯網
上載者:User
{foreach}is used to loop over an associative arrayas 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 name attribute.
  • {foreach}迴圈也有自身屬性的變數,可以通過{$smarty.foreach.name.property}訪問,其中”name”是name屬性。

    Note: The nameattribute 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 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 $myArrayin an un-ordered list

用模板以無序列表輸出$myArray

 
    {foreach from=$myArray item=foo}
  • {$foo}
  • {/foreach}

The above example will output:

上例將輸出:

 
  • 1000
  • 1001
  • 1002

Example 7-6. Demonstrates the item and key attributes

例 7-6. 示範item和key屬性

      'Tennis', 3 => 'Swimming', 8 => 'Coding');$smarty->assign('myArray', $arr);?>

Template to output $myArrayas key/val pair, like PHP’s foreach .

用模板按鍵名/索引值對的形式輸出$myArray, 類似於PHP的foreach。

 
    {foreach from=$myArray key=k item=v}
  • {$k}: {$v}
  • {/foreach}

The above example will output:

上例將輸出:

 
  • 9: Tennis
  • 3: Swimming
  • 8: Coding

Example 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);?>

Template to output $itemswith $myIdin 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設定一個數組,對於每個鍵名對應的每個迴圈值都包括鍵。

     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}

The above example will output:

上例將輸出:

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

Example 7-9. Database example with {foreachelse}

例 7-9. 使用{foreachelse}的 資料庫樣本

A database (eg PEAR or ADODB) example of a search script, the query results assigned to Smarty

一個資料庫(例如PEAR或ADODB)的搜尋指令碼樣本,

     assign('results', $db->getAssoc($sql) );?>

The template which display “None found” if no results with {foreachelse}.

藉助{foreachelse}標記在沒有結果時模板輸出”None found”字樣。

{foreach key=cid item=con from=$results}    {$con.name} - {$con.nick}
{foreachelse} No items were found in the search{/foreach}

indexcontains the current array index, starting with zero.

.index包含當前數組索引,從零開始。

Example 7-10. index example

例 7-10. index樣本

{* The header block is output every five rows *}{* 每五行輸出一次頭部區塊 *}                             
 
                                   {foreach from=$items key=myId item=i name=foo}{if $smarty.foreach.foo.index % 5 == 0}                                                    
  {/if} 
   {/foreach} 
  
Title
{$i.label}

.iteration

iterationcontains the current loop iteration and always starts at one, unlike index . It is incremented by one on each iteration.

iteration包含當前迴圈次數,與index不同,從1開始,每次迴圈增長1。

Example 7-11. iteration and index example

例 7-11. iteration和index樣本

{* this will output 0|1, 1|2, 2|3, ... etc *}{* 該例將輸出0|1, 1|2, 2|3, ... 等等 *}{foreach from=$myArray item=i name=foo}{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},{/foreach}

firstis TRUEif the current {foreach}iteration is the initial one.

first在當前{foreach}迴圈處於初始位置時值為TRUE。

Example 7-12. first property example

例 7-12. first屬性樣本

{* show LATEST on the first item, otherwise the id *}{* 對於第一個條目顯示LATEST而不是id *}                             
 
                                   {foreach from=$items key=myId item=i name=foo}                                                    
  {/foreach} 
  
{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if} {$i.label}

lastis set to TRUEif the current {foreach}iteration is the final one.

last在當前{foreach}迴圈處於最終位置是值為TRUE。

Example 7-13. last property example

例 7-13. last屬性樣本

{* Add horizontal rule at end of list *}{* 在列表結束時增加一個水平標記 *}){foreach from=$items key=part_id item=prod name=products}             {$prod}{if $smarty.foreach.products.last}                              {else},{/if}{foreachelse}... content ...{/foreach}      

showis used as a parameter to {foreach}. showis 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},將被顯示。

totalcontains the number of iterations that this {foreach}will loop. This can be used inside or after the {foreach}.

total包括{foreach}將迴圈的次數,既可以在{foreach}中使用,也可以在之後使用。

Example 7-14. total property example

例 7-14. total屬性樣本

{* show rows returned at end *}{* 在結束位置顯示行數 *}{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。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.