<?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}

smarty foreach 最全用法的更多相关文章

  1. smarty -- foreach用法

    {foreach},{foreachelse} 用于像访问序数数组一样访问关联数组 {foreach},{foreachelse} {foreach} is used to loop over an  ...

  2. smarty -- foreach用法详解

    {foreach},{foreachelse} 用于像访问序数数组一样访问关联数组 {foreach},{foreachelse} {foreach} is used to loop over an  ...

  3. ecshop中foreach的详细用法归纳

    ec模版中foreach的常见用法. foreach 语法: 假如后台:$smarty->assign('test',$test); {foreach from=$test item=list ...

  4. PHP模板引擎Smarty内建函数section,sectionelse用法详解

    本文实例讲述了PHP模板引擎Smarty内建函数section,sectionelse用法.分享给大家供大家参考,具体如下: section 是 Smarty 模板中除了 foreach 以外的另一种 ...

  5. smarty foreach循环

    1,smarty foreach1,单纯的数组array(1000,2000,3000),使用foreach(from = $array item=foo){$foo}2,键值对数组<ul> ...

  6. (转)Smarty Foreach 使用说明

    foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案). foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是 ...

  7. C# 和Java的foreach的不同用法

    循环语句为苦逼的程序猿们提供了很大的便利,有while.do...while.for和 foreach.而且foreach语句很简洁,但是它的优点不仅仅在于此,它的效率也是最高的. 作为两个开发语言, ...

  8. c#--foreach遍历的用法与split的用法

    一. foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成.in右边的项是集合名,in左边的项是变量名,用来存放该集合中的每个元素.      该循环 ...

  9. php foreach函数的用法

    php foreach函数用法举例.  Foreach 函数(PHP4/PHP5) foreach 语法结构提供了遍历数组的简单方式. foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类 ...

随机推荐

  1. 关于ios对rtsp格式的流媒体支持的一些官方说明

    ios明确不支持rtsp格式的流媒体,基于rtsp/rtp对通用性和防炎墙以及需要开新端口等额外影响稳定性和通用性的原因. 而对http流的视频支持是最好的.虽然有第三方的方式配合ffmpeg库,实现 ...

  2. OpenMP多线程linux下的使用,简单化

    http://hi.baidu.com/diwulechao/item/bc6d865c411b813c32e0a932 http://www.cnblogs.com/yangyangcv/archi ...

  3. NDIS IM 驱动那些事情

    最近不知道为什么开始学习windows NDIS驱动开发,用的是寒江独钓的例子,其实他的改的代码也就一点点,说的有用的东西也就那么多,不过还是感谢他出了这么一本书,不然这真的就没有一本稍微好一点的书籍 ...

  4. 如何把网站及数据库部署到Windows Azure

    http://edi.wang/Post/2014/1/1/deploying-website-with-db-to-azure-custom-domain

  5. tinyMCE自定义添加图片插件

    需求: 在富文本编辑器中插入图片,图片来自用户可以自己上传的图片库. 本来可以用比较恶心的方式,也就是直接用tinyMCE自带的插入图片插件来实现.恶心是因为这个图片插件需要用户填入图片的url. 想 ...

  6. 完美高仿精仿京东商城手机客户端android版源码

    完美高仿精仿京东商城手机客户端android版源码,是从安卓教程网那边转载过来的,这款应用源码非常不错的,也是一个非常优秀的应用源码的,希望能够帮到学习的朋友. _js_op> <igno ...

  7. C# 实现HTML5服务器推送事件

    为什么需要服务器推送事件: 因为如果需要保持前台数据的实时更新例如,IM聊天,股票信息, 1.可以在客户端不断地调用服务端的方法来获得新数据,但是这样会很消耗服务器资源,导致系统变慢! 2 html5 ...

  8. ligerGrid 取得选中行的数据

    var SelectUserIdArr = []; function GetData() { var returnValue = ""; var grid = $("#m ...

  9. if...else..的错误用法

    1.最近在写js代码完成一个前段DOM操作的函数时,自己错误的使用了if..else..控制体.为什么是错误的呢?看看我的 代码你就明白了: document.getElementsByClassNa ...

  10. fedora 禁止nouveau加载

    To remove / disable nouveau drivers from kernel initramfs ## Backup old initramfs nouveau image ## m ...