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

转载于:https://www.cnblogs.com/sandea/p/3736453.html

smarty foreach 最全用法相关推荐

  1. php模板引擎循环start,PHP模板引擎Smarty内建函数section,sectionelse用法详解

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

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

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

  3. ecshop模板smarty foreach详解 [ECshop]

    {foreach},{foreachelse} {foreach} 用于像循环访问一个数字索引数组一样循环访问一个关联数组,与仅能访问数字索引数组的{section}不同,{foreach}的语法比 ...

  4. smarty foreach函数

    {foreach},{foreachelse} Attribute Name属性名称 Type类型 Required必要 Default默认值 Description描述 from array数组 Y ...

  5. Smarty foreach

    2019独角兽企业重金招聘Python工程师标准>>> Smarty - Manua手册 - Chapter 7. Built-in Functions第7章 内建函数 - {for ...

  6. cshop模板smarty foreach详解

    {foreach},{foreachelse} {foreach} 用于像循环访问一个数字索引数组一样循环访问一个关联数组,与仅能访问数字索引数组的{section}不同,{foreach}的语法比 ...

  7. ecshop模板smarty foreach详解

    Smarty目录: /libs Smarty的功能类目录 /tempalates 模板文件目录 /templates_c 模板缓存文件目录 /configs 配置文件目录 /cache 缓存文件目录 ...

  8. Python中下划线 _ 的最全用法详解

    Python中下划线 _ 的最全用法详解 '_'是什么? _在python中可以作为一个标识符,用于定义变量和方法唯一名称.同时它也是Python中的一个软关键字,指在某些特定上下文中保留的关键字.截 ...

  9. Smarty foreach 循环次数 首次 末次

    foreach,foreachelse {foreach from=$variable key=key name=name iteam=value}{$variable|@count} // 获取数组 ...

最新文章

  1. 看板中的WIP限制思想
  2. 解决不了“不可能三角”,火山抖音化只是个昏招
  3. 5种Python逐行读取文件的方式
  4. 7-20牛客网未知点集合
  5. 15款提高工作效率的工具分享
  6. 厦门理工学院2019年数据结构与算法考研初试大纲
  7. iPhone 12在二季度iPhone出货量中占63% 低于iPhone 11同期
  8. AWS API Gateway Swagger定义
  9. 设置ctfmon.exe自动启动
  10. cc1101 使用笔记
  11. 批量将多个 txt 记事本文件合并成一个独立的记事本文件
  12. 【图像处理】海森矩阵(Hessian Matrix)及一个用例(图像增强)
  13. TA_Lib库指标中文说明
  14. 这知道这个错误是咋回事啊?
  15. 计算机专业专科可以进的国企,专科毕业想要进国企?这四大专业是首选,成功率高达50%...
  16. RxSwift之路01-----简单的RxSwift使用
  17. 四足鼎立,快递业的变局前夜
  18. 人人都想自学python_人人都想自学编程,为什么坚持下来的没几个?
  19. (转载)机器学习方法的PPT
  20. 医疗供应链安全领域的相关调查(7篇相关论文简介)

热门文章

  1. boost--windows平台下针对VS的简单编译
  2. C++_函数_函数的占位参数_函数重载---C++语言工作笔记034
  3. 大数据之-Hadoop之HDFS_HDFS的内容介绍---大数据之hadoop工作笔记0047
  4. JAVA线程池_并发队列工作笔记0004---Callable原理_多线程执行Callable任务
  5. 找不到该项目,请确认该项目的位置的解决办法
  6. 微信小程序开发学习笔记002--微信小程序框架解密
  7. My Ruby Blog
  8. 将Notepad++配置为Ruby编译器
  9. java分布式(java反汇编)
  10. linux 不显示路径原因,linux 下shell 显示-bash-4.1不显示路径解决办法