smarty的foreach使用详解

讲如何使用smarty foreach使用 如:{foreach from=$myArray item=foo}

  • {$foo}
  • {/foreach}循环$myArray数组,打印值{$foo}出来

    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循环的名称

    例 7-5. item属性

    1. $arr = array("http://www.phpzy.com", "www.phpzy.com", "绿色php资源");  
    2. $smarty->assign('myArray', $arr);  
    3. ?> 

    Template to output $myArray in an un-ordered list

    用模板以无序列表输出$myArray

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

    The above example will output:

    上例将输出:

    Example 7-6. Demonstrates the item and key attributes

    例 7-6. 演示item和key属性

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

    Template to output $myArray as key/val pair, like PHP's foreach.

    用模板按键名/键值对的形式输出$myArray, 类似于PHP的foreach。

      •  
      • {foreach from=$myArray kkey=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属性是关联数组

    $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

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

    $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的模板。

    1. {foreach name=outer item=contact from=$contacts}     
    2. {foreach key=key item=item from=$contact}      
    3. {$key}: {$item}  
    4. {/foreach}  
    5. {/foreach}

    The above example will output:

    上例将输出:


    1.     
    2. phone: 1  
    3. fax: 2  
    4. cell: 3

       
    5. phone: 555-4444
         
    6. fax: 555-3333  
    7. 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)的搜索脚本示例,

      $search_condition = "where name like '$foo%' ";
    $sql = 'select contact_id, name, nick from contacts '.$search_condition.' order by name';
    $smarty->assign('results', $db->getAssoc($sql) );
    ?>

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

    借助{foreachelse}标记在没有结果时模板输出"None found"字样。

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

    .index

    index contains 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

    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。

    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}

    .first

    first is TRUE if 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}

    .last

    last is set to TRUE if 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}

    .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}中使用,也可以在之后使用。

    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}

    原文地址:https://www.cnblogs.com/lechie/p/2383238.html