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}. php

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

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

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

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

{foreachelse}       this

No items were found in the search   spa

{/foreach}  索引

.index

index contains the current array index, starting with zero. ci

.index包含当前数组索引,从零开始。 rem

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(当前 foreach 循环第一次执行时 first 被设置成 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(当前 foreach 循环执行到最后一遍时 last 被设置成 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},将被显示show 是 foreach 的一个参数. 取值为布尔值 true 或 false. 若是指定为 false 该循环不显示,若是循环指定了foreachelse 子句,该子句显示与否也取决于 show 的取值)。

.total

total contains the number of iterations that this {foreach} will loop. This can be used inside or after the {foreach}.

total包括{foreach}将循环的次数,既能够在{foreach}中使用,也能够在以后使用(total 用于显示循环执行的次数,能够在循环中或循环执行后调用)。

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}
相关文章
相关标签/搜索