• smarty foreach 最全用法


    <?php
    $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"字样。

    {foreach key=cid item=con from=$results}     

     <a href="contact.php?contact_id={$cid}"> 

    {$con.name} - {$con.nick}</a><br /> 

    {foreachelse}      

    No items were found in the search  

    {/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 *}
    {* 每五行输出一次头部区块 *}
    <table>
    {foreach from=$items key=myId item=i name=foo}
    {if $smarty.foreach.foo.index % 5 == 0}
    <tr><th>Title</th></tr>
    {/if}
    <tr><td>{$i.label}</td></tr>
    {/foreach}
    </table>
    .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 *}
    <table>
    {foreach from=$items key=myId item=i name=foo}
    <tr>
    <td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td>
    <td>{$i.label}</td>
    </tr>
    {/foreach}
    </table>
    .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}
    <a href="#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{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><hr/>
    {if $smarty.foreach.foo.last}
    <div id="total">{$smarty.foreach.foo.total} items</div>
    {/if}
    {foreachelse}
    ... something else ...
    {/foreach}
  • 相关阅读:
    HDU 5273 区间DP
    【管理心得之八】通过现象看本质,小王和小张谁更胜任?
    【Unity 3D】学习笔记四十二:粒子特效
    linux kernel的cmdline參数解析原理分析
    adoquery.refresh和adoquery.query的区别
    Delphi中的Sender:TObject对象解析
    ADODataSet与ADOQuery的区别
    visual studio 和 sql server 的激活密钥序列号
    修改VCL源码实现自定义输入对话框
    WINFORM 多条件动态查询 通用代码的设计与实现
  • 原文地址:https://www.cnblogs.com/sandea/p/3736453.html
Copyright © 2020-2023  润新知