zoukankan      html  css  js  c++  java
  • Smarty模板技术之foreach遍历数组实例全面讲解

    一、item属性用法
    
    <?php
    $arr = array(600, 851, 7412);
    $smarty->assign('testarrg', $arr);
    ?>
    
    用Smarty中的foreach方法来遍历并输出这个数组
    
    <dl>
    <dt>foreach中item属性用法</dt>
    {foreach from=$testarrg item=test}
    <dd>{$test}</dd>
    {/foreach}
    </dl>
    
    通过上面的将输出:
    
    <dl>
    <dt>foreach中item属性用法</dt>
    <dl>600</dl>
    <dl>851</dl>
    <dl>7412</dl>
    </dl>
    二、关于item和key属性的演示
    
    <?php
    $arr = array(1 => 'test1', 2 => 'test2', 3=> 'test3');
    $smarty->assign('testarray', $arr);
    ?>
    
    上面的实例是按照数组的键/值对形式的
    
    用模板按键名/键值对的形式输出$testarray, 类似于PHP的foreach。
    
    <dl>
    <dt>foreach中item和key属性用法</dt>
    {foreach from=$testarray key=key item=value}
    <dd>{$key}: {$value}</dd>
    {/foreach}
    </dl>
    
    上面的例子中将输出:
    
    <dl>
    <dt>foreach中item和key属性用法</dt>
    <dd>1: test1</dd>
    <dd>2: test1</dd>
    <dd>3: test2</dd>
    </dl>
    三、关于{foreach}中的item属性是关联数组的演示案例
    
    <?php
    $test_list=array(10=>array('a'=>1256, 'b'=>'testok'),
    11=>array('a'=>8959, 'b'=>'oktest'));
    $smarty->assign('arr_item', $test_list);
    ?>
    在TPL模板中写入foreach循环的方法
    <ul>
    {foreach from=$test_list key="key" item="val"}
    <li>{$val.a}: {$val.b}</li>
    {/foreach}
    </ul>
    上例将输出:
    
    <ul>
    <li>1256: testok</li>
    <li>8959: oktest</li>
    </ul>
    四、{foreach}使用嵌套的item和key
     
    
    Assign an array to Smarty, the key contains the key for each looped value.
    
    向Smarty设置一个数组,对于每个键名对应的每个循环值都包括键。
    <?php
    $_arrg=array(array('photo'=>100, 'phfax'=>200, 'phcell'=>300),
    array('phone'=>'Hello', 'fax'=>'Word', 'cell'=>'Hess'));
    $smarty->assign('arrg', $_arrg);
    ?>
    
    用foreach向输模板中输出
    
    {foreach name="outgow" item="ct" from=$arrg}
    <hr />
    {foreach key="keys" item="ims" from=$arrg}
    {$keys}: {$ims}<br />
    {/foreach}
    {/foreach}
    
    上例将输出:
    
    <hr />
    photo: 100<br />
    phfax: 200<br />
    phcell: 300<br />
    <hr />
    phone: Hello<br />
    fax: Word<br />
    cell: Hess<br />
    五、关于foreach的属性index的用法
    
    {* 三行就输出一次头部 *}
    <table width="500" boder="1">
    {foreach from=$items key=myId item="is" name="fot"}
    {if $smarty.foreach.fot.index % 3 == 0} //除3余数0
    <tr><th>文章标题</th></tr>
    {/if}
    <tr><td>{$is.label}</td></tr>
    {/foreach}
    </table>
    六、关于foreach的属性iteration的用法
    
    iteration显示当前循环次数,它和index属性不同,它是每次从1开始,每次循环增长1。 www.it165.net
    
    {foreach from=$myArray item=i name=foo}
    {$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
    {/foreach}
    七、关于foreach的属性first的用法
    
    first属性在foreach循环第一次执行时返回真
    
    {* 对于第一个条目显示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>
    八、关于foreach的属性last的用法
    
    lase属性在foreach循环最后一次执行时返回真
    
    {* 在列表结束时增加一个水平标记 *})
    {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}
    九、关于foreach的属性show的用法
    
    show属性返回的是一个布尔值,当它为真(ture)的时候,该循环就显示,它不为假(false)的时候该循环就不显示。
  • 相关阅读:
    TranslateAnimation详解
    商业价值:谷歌娱乐影音之路上的硬件产品
    红黑树源码实现
    cocos2d-x Loading界面实现资源加载
    Android String 转 MD5
    [置顶] 【原创分享】嵌入式linux应用之内核移植定制篇-前篇(linux-3.8.12 mini2440)--20130824
    搭建Windows故障转移群集
    ,典型递归问题-F(1025)mod 5 的值
    android:为TextView加入样式——下划线,颜色,设置链接样式及前背景色
    现在仍在工作的12名最“屌”的程序猿
  • 原文地址:https://www.cnblogs.com/kangshuai/p/4827972.html
Copyright © 2011-2022 走看看