Get Smarty

Donate

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap glasses from Cheapglasses123.com and save up to 80%.

Where to buy prescription glasses? EyewearCanada.com is your best choice!

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Shop for 2018 World Cup Football Boots Online sale at guthjcc.com

Browse best 2018 world cup soccer cleats and shoes from SoccerAzteca.com

Advertisement

{foreach},{foreachelse}

{foreach}用于循环数组。 {foreach}的语法比{section}循环要更简单和清晰,并且可以使用非数字下标的数组。

{foreach $arrayvar as $itemvar}

{foreach $arrayvar as $keyvar=>$itemvar}

Note

foreach的语法可以接受没有名称的属性,该语法是Smarty 3新增的。然而Smarty 2语法 {foreach from=$myarray key="mykey" item="myitem"}也同样支持。

  • {foreach} 循环可以被嵌套使用.

  • array变量,一般是数组的值,决定了{foreach} 循环的次数。你也可以传递一个任意的整数来控制循环次数。

  • 如果array数组变量中,没有值的情况下, {foreachelse}将执行。

  • {foreach}的属性: @index, @iteration, @first, @last, @show, @total.

  • {foreach}的语法命令: {break}, {continue}.

  • 代替指定key变量,你可以通过 {$item@key}来使用循环的当前key。(见下面的例子).

Note

$var@property的语法是Smarty 3新增的。然而Smarty 2风格的语法{foreach from=$myarray key="mykey" item="myitem"}, $smarty.foreach.name.property也是支持的。

Note

即使你在循环语法里{foreach $myArray as $myKey => $myValue} 已经指定了key的变量名,但循环体内$myValue@key还是可用的。

可选标记:

名称 说明
nocache 关闭{foreach}循环的缓存

Example 7.30. 简单的{foreach} 循环


<?php
$arr = array('red', 'green', 'blue');
$smarty->assign('myColors', $arr);
?>

  

模板将顺序输出$myColors


<ul>
{foreach $myColors as $color}
    <li>{$color}</li>
{/foreach}
</ul>

  

输出:


<ul>
    <li>red</li>
    <li>green</li>
    <li>blue</li>
</ul>

  

Example 7.31. 使用key变量的例子


<?php
$people = array('fname' => 'John', 'lname' => 'Doe', 'email' => 'j.doe@example.com');
$smarty->assign('myPeople', $people);
?>

  

模板将以键值对的方式输出$myArray


<ul>
{foreach $myPeople as $value}
   <li>{$value@key}: {$value}</li>
{/foreach}
</ul>

  

输出:


<ul>
    <li>fname: John</li>
    <li>lname: Doe</li>
    <li>email: j.doe@example.com</li>
</ul>

  

Example 7.32. 多维数组通过itemkey来嵌套使用{foreach}

多维数组的键一般会对应另一个数组。


<?php
 $smarty->assign('contacts', array(
                             array('phone' => '555-555-1234',
                                   'fax' => '555-555-5678',
                                   'cell' => '555-555-0357'),
                             array('phone' => '800-555-4444',
                                   'fax' => '800-555-3333',
                                   'cell' => '800-555-2222')
                             ));
?>

  

模板将输出$contact.


{* key always available as a property *}
{foreach $contacts as $contact}
  {foreach $contact as $value}
    {$value@key}: {$value}
  {/foreach}
{/foreach}

{* accessing key the PHP syntax alternate *}
{foreach $contacts as $contact}
  {foreach $contact as $key => $value}
    {$key}: {$value}
  {/foreach}
{/foreach}

  

上面两个例子都会输出:


  phone: 555-555-1234
  fax: 555-555-5678
  cell: 555-555-0357
  phone: 800-555-4444
  fax: 800-555-3333
  cell: 800-555-2222

  

Example 7.33. {foreachelse}的数据库例子

循环显示数据库(PDO)结果。例子是循环了一个PHP的迭代器(iterator)而不是一个数组(array)。


<?php 
  include('Smarty.class.php'); 

  $smarty = new Smarty; 

  $dsn = 'mysql:host=localhost;dbname=test'; 
  $login = 'test'; 
  $passwd = 'test'; 

  // setting PDO to use buffered queries in mysql is 
  // important if you plan on using multiple result cursors 
  // in the template. 

  $db = new PDO($dsn, $login, $passwd, array( 
     PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)); 

  $res = $db->prepare("select * from users"); 
  $res->execute(); 
  $res->setFetchMode(PDO::FETCH_LAZY); 

  // assign to smarty 
  $smarty->assign('res',$res); 

  $smarty->display('index.tpl');?>
?>

  

{foreach $res as $r} 
  {$r.id} 
  {$r.name}
{foreachelse}
  .. no results .. 
{/foreach}

  

上面的例子显示了查询结果idname两个字段的内容。

迭代器和数组循环哪个更高效呢?数组而言,每次循环之前全部的数组数据都会被先放到内存堆栈内,然后再进行循环。 而迭代器每次迭代循环时,都会载入并释放结果内容,这样可以节省运行时间和内存,尤其是当结果集非常大的时候。

@index

index是当前数组索引,从0开始计算。

Example 7.34. index 例子


{* output empty row on the 4th iteration (when index is 3) *}
<table>
{foreach $items as $i}
  {if $i@index eq 3}
     {* put empty table row *}
     <tr><td>nbsp;</td></tr>
  {/if}
  <tr><td>{$i.label}</td></tr>
{/foreach}
</table>

  

@iteration

iteration是当前循环的次数,和index不同,iteration是从1开始。 iteration在每次循环的时候都会加一。

Example 7.35. iteration 例子: is div by

"is div by"运算可以对循环次数做一些特殊的判断。 下面我们将每4次循环就输出一次粗体的名称。


{foreach $myNames as $name}
  {if $name@iteration is div by 4}
    <b>{$name}</b>
  {/if}
  {$name}
{/foreach}


Example 7.36. iteration 例子: is even/odd by

"is even by""is odd by"可以用于在循环中奇偶交替进行一些操作。在开始的时候可以选择奇或偶的循环。 下面是每三次循环会改变一次字体颜色。

 
 {foreach $myNames as $name}
   {if $name@iteration is even by 3}
     <span style="color: #000">{$name}</span>
   {else}
     <span style="color: #eee">{$name}</span>
   {/if}
 {/foreach}
 
 

输出:


    <span style="color: #000">...</span>
    <span style="color: #000">...</span>
    <span style="color: #000">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #000">...</span>
    <span style="color: #000">...</span>
    <span style="color: #000">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #eee">...</span>
    ...

   

@first

当循环{foreach}是首次循环时,first将为TRUE 下面我们用first来显示一个表格的表头。

Example 7.37. first例子


{* show table header at first iteration *}
<table>
{foreach $items as $i}
  {if $i@first}
    <tr>
      <th>key</td>
      <th>name</td>
    </tr>
  {/if}
  <tr>
    <td>{$i@key}</td>
    <td>{$i.name}</td>
  </tr>
{/foreach}
</table>

  

@last

{foreach}循环到了最后一次时, last将为TRUE。 下面我们将在循环的最后插入一条水平线。

Example 7.38. last例子


{* Add horizontal rule at end of list *}
{foreach $items as $item}
  <a href="#{$item.id}">{$item.name}</a>{if $item@last}<hr>{else},{/if}
{foreachelse}
  ... no items to loop ...
{/foreach}

  

@show

show属性是在{foreach}循环执行之后, 检测循环是否显示数据的判断。 show是一个布尔值。

Example 7.39. show例子


<ul>
{foreach $myArray as $name}
    <li>{$name}</li>
{/foreach}
</ul>
{if $name@show} do something here if the array contained data {/if}


@total

total是整个{foreach}循环的次数。 total可以在{foreach}内部,或者之后使用。

Example 7.40. total例子


{* show number of rows at end *}
{foreach $items as $item}
  {$item.name}<hr/>
  {if $item@last}
    <div id="total">{$item@total} items</div>
  {/if}
{foreachelse}
 ... no items to loop ...
{/foreach}


参见{section}, {for}{while}

{break}

{break}停止循环。

Example 7.41. {break} 例子

 
  {$data = [1,2,3,4,5]}
  {foreach $data as $value}
    {if $value == 3}
      {* abort iterating the array *}
      {break}
    {/if}
    {$value}
  {/foreach}
  {*
    prints: 1 2
  *}
 
   

{continue}

{continue}将跳过当前本次循环并进入下一次循环。

Example 7.42. {continue} 例子

 
  {$data = [1,2,3,4,5]}
  {foreach $data as $value}
    {if $value == 3}
      {* skip this iteration *}
      {continue}
    {/if}
    {$value}
  {/foreach}
  {*
    prints: 1 2 4 5
  *}
 
   

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 19 plus 2? (Are you human?)

Advertisement