{section}可以循环遍历
  连续数字索引的数组,
  区别于{foreach}
  可以循环任意关联数组.
  每个{section}标签都必须有一个匹配的{/section}关闭标签。
   
   {foreach}
   可以做到任何{section}做到的功能, 而且更简单和有更清晰的语法。一般更推荐使用{foreach}语法.
 
   {section}不能用于循环关联数组,它仅能循环数字索引的、连续下标的 (0,1,2,...)数组。
   要循环关联数组,请使用{foreach}。
 
| 参数名称 | 类型 | 必选参数 | 默认值 | 说明 | 
|---|---|---|---|---|
| name | string | Yes | n/a | section的名称 | 
| loop | mixed | Yes | n/a | 用于循环的值 | 
| start | integer | No | 0 | 设置开始循环的下标。如果设置成负值,则会从数组的结束位置开始。 比如说,如果数组中有7个元素,设置该值为-2,则循环将从下标5开始。 设置了不正确的值(比如说在数组长度以外的值)那么会自动计算为最接近的值。 | 
| step | integer | No | 1 | 循环的步长值。比如,step=2将循环下标0,2,4,等。 如果step值设置成负数,那么将从最后开始计算步长。 | 
| max | integer | No | n/a | 设置最大的循环次数。 | 
| show | boolean | No | TRUE | 
是否显示循环内容 | 
可选标记:
| 名称 | 说明 | 
|---|---|
| nocache | 关闭{section}缓存 | 
   name 和 loop是必须的参数。
   
   {section}的name可以是任意字符,如数字、字母或下划线等,和
   PHP 变量一样的命名规则。
    
    {section}可以嵌套,而且嵌套的{section}名称必须唯一。
   
   loop一般是数组,决定了{section}的循环次数。
   同时你也可以传递一个整数指定循环次数。
   
当在{section}内显示变量时, 
   {section} 的name必须给变量名称加上[方括号].
   
   如果loop属性为空,{sectionelse}将执行。
 	
  {section}同时还有自己的属性。
  这些属性都是通过: 
  {$smarty.section.name.property}来使用,其中“name”是name属性.
  
  {section}的内置属性有:
  index,
  index_prev,
  index_next,
  iteration,
  first,
  last,
  rownum,
  loop,
  show,
  total.
  
Example 7.63. 简单的{section}例子
assign()赋值一个数组
<?php
$data = array(1000,1001,1002);
$smarty->assign('custid',$data);
?>
模板将输出该数组
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
{section customer $custid} {* short-hand *}
  id: {$custid[customer]}<br />
{/section}
<hr />
{*  print out all the values of the $custid array reversed *}
{section name=foo loop=$custid step=-1}
{section foo $custid step=-1} {* short-hand *}
  {$custid[foo]}<br />
{/section}
  
输出:
id: 1000<br /> id: 1001<br /> id: 1002<br /> <hr /> id: 1002<br /> id: 1001<br /> id: 1000<br />
Example 7.64. {section}不使用赋值数组
{section name=foo start=10 loop=20 step=2}
  {$smarty.section.foo.index}
{/section}
<hr />
{section name=bar loop=21 max=6 step=-2}
  {$smarty.section.bar.index}
{/section}
输出:
10 12 14 16 18 <hr /> 20 18 16 14 12 10
Example 7.65. 给{section}设置名称
{section}的 name属性可以是任意字符,请参见PHP 变量定义.
  它是用于引用{section}的数据.
{section name=anything loop=$myArray}
  {$myArray[anything].foo}
  {$name[anything]}
  {$address[anything].bar}
{/section}
  
Example 7.66. {section}中使用关联数组
下面是使用{section}来输出关联数组的例子。
  这里是在PHP代码中赋值$contacts 数组到Smarty。
  
<?php
$data = array(
          array('name' => 'John Smith', 'home' => '555-555-5555',
                'cell' => '666-555-5555', 'email' => 'john@myexample.com'),
          array('name' => 'Jack Jones', 'home' => '777-555-5555',
                'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
          array('name' => 'Jane Munson', 'home' => '000-555-5555',
                'cell' => '123456', 'email' => 'jane@myexample.com')
        );
$smarty->assign('contacts',$data);
?>
  
该模板用于显示$contacts
{section name=customer loop=$contacts}
<p>
  name: {$contacts[customer].name}<br />
  home: {$contacts[customer].home}<br />
  cell: {$contacts[customer].cell}<br />
  e-mail: {$contacts[customer].email}
</p>
{/section}
  
输出:
<p> name: John Smith<br /> home: 555-555-5555<br /> cell: 666-555-5555<br /> e-mail: john@myexample.com </p> <p> name: Jack Jones<br /> home phone: 777-555-5555<br /> cell phone: 888-555-5555<br /> e-mail: jack@myexample.com </p> <p> name: Jane Munson<br /> home phone: 000-555-5555<br /> cell phone: 123456<br /> e-mail: jane@myexample.com </p>
Example 7.67. {section}的loop属性的演示例子
例子假定$custid, $name
  和 $address三个数组中对应的值都有着相同的数字下标。
  首先从PHP代码中赋值到Smarty
<?php
$id = array(1001,1002,1003);
$smarty->assign('custid',$id);
$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);
$addr = array('253 Abbey road', '417 Mulberry ln', '5605 apple st');
$smarty->assign('address',$addr);
?>
  loop值仅是指定循环的次数。
  你可以在{section}中给它设置任何的变量。
  在多个数组循环时比较有用。
  你可以传递一个数组来让其计算总数而指定循环次数,也可以直接指定一个循环次数的整数。
  
{section name=customer loop=$custid}
<p>
  id: {$custid[customer]}<br />
  name: {$name[customer]}<br />
  address: {$address[customer]}
</p>
{/section}
  
输出:
<p> id: 1000<br /> name: John Smith<br /> address: 253 Abbey road </p> <p> id: 1001<br /> name: Jack Jones<br /> address: 417 Mulberry ln </p> <p> id: 1002<br /> name: Jane Munson<br /> address: 5605 apple st </p>
Example 7.68. {section}嵌套
  {section}可以嵌套任意的深度。通过嵌套{section}你可以处理多维数组。
  下面是例子的.php文件。
  
<?php
$id = array(1001,1002,1003);
$smarty->assign('custid',$id);
$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);
$addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');
$smarty->assign('address',$addr);
$types = array(
           array( 'home phone', 'cell phone', 'e-mail'),
           array( 'home phone', 'web'),
           array( 'cell phone')
         );
$smarty->assign('contact_type', $types);
$info = array(
           array('555-555-5555', '666-555-5555', 'john@myexample.com'),
           array( '123-456-4', 'www.example.com'),
           array( '0457878')
        );
$smarty->assign('contact_info', $info);
?>
  
在这个模板里, $contact_type[customer]是客户联系信息的数组
{section name=customer loop=$custid}
<hr>
  id: {$custid[customer]}<br />
  name: {$name[customer]}<br />
  address: {$address[customer]}<br />
  {section name=contact loop=$contact_type[customer]}
    {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br />
  {/section}
{/section}
  
输出:
<hr>
  id: 1000<br />
  name: John Smith<br />
  address: 253 N 45th<br />
    home phone: 555-555-5555<br />
    cell phone: 666-555-5555<br />
    e-mail: john@myexample.com<br />
<hr>
  id: 1001<br />
  name: Jack Jones<br />
  address: 417 Mulberry ln<br />
    home phone: 123-456-4<br />
    web: www.example.com<br />
<hr>
  id: 1002<br />
  name: Jane Munson<br />
  address: 5605 apple st<br />
    cell phone: 0457878<br />
  
Example 7.69. {sectionelse}的数据库例子
数据库查找的结果(如 ADODB 或 PEAR) 传递到 Smarty
  
<?php
$sql = 'select id, name, home, cell, email from contacts '
      ."where name like '$foo%' ";
$smarty->assign('contacts', $db->getAll($sql));
?>
模板将以表格形式显示数据结果
<table>
<tr><th> </th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
{section name=co loop=$contacts}
  <tr>
    <td><a href="view.php?id={$contacts[co].id}">view<a></td>
    <td>{$contacts[co].name}</td>
    <td>{$contacts[co].home}</td>
    <td>{$contacts[co].cell}</td>
    <td>{$contacts[co].email}</td>
  <tr>
{sectionelse}
  <tr><td colspan="5">No items found</td></tr>
{/section}
</table>
   index是当前数组的索引值,从0开始,或者从设定的start值开始。它将每次循环增加1或者增加指定的step值。
  
    如果 step 和 start都没有被指定,
	那么它会和iteration属性很像, 只不过它是从0开始,而iteration是从1开始.
   
Example 7.70. {section} index 属性
$custid[customer.index] 和
$custid[customer] 是一样的.
{section name=customer loop=$custid}
  {$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
  
输出:
0 id: 1000<br /> 1 id: 1001<br /> 2 id: 1002<br />
   index_next是下一次循环的索引值。
   在最后一次循环时,它会比当前索引加1,或者加上指定的step属性值。
  
Example 7.71. index, index_next
 和 index_prev 属性 
<?php
$data = array(1001,1002,1003,1004,1005);
$smarty->assign('rows',$data);
?>
在表格中显示数组
{* $rows[row.index] and $rows[row] are identical in meaning *}
<table>
  <tr>
    <th>index</th><th>id</th>
    <th>index_prev</th><th>prev_id</th>
    <th>index_next</th><th>next_id</th>
  </tr>
{section name=row loop=$rows}
  <tr>
    <td>{$smarty.section.row.index}</td><td>{$rows[row]}</td>
    <td>{$smarty.section.row.index_prev}</td><td>{$rows[row.index_prev]}</td>
    <td>{$smarty.section.row.index_next}</td><td>{$rows[row.index_next]}</td>
  </tr>
{/section}
</table>
  
输出:
index id index_prev prev_id index_next next_id 0 1001 -1 1 1002 1 1002 0 1001 2 1003 2 1003 1 1002 3 1004 3 1004 2 1003 4 1005 4 1005 3 1004 5
   iteration是当前的循环次数,从1开始。
  
    它和index不同,不会受到{section} 的属性
    start, step 和 max等影响. 
	而且和index不同的是,iteration还是从1开始计算的。 rownum是
    iteration的别名,它们是一样的。
   
Example 7.72. iteration属性 
<?php
// array of 3000 to 3015
$id = range(3000,3015);
$smarty->assign('arr',$id);
?>
模板将按step=2来显示$arr的数组元素
{section name=cu loop=$arr start=5 step=2}
  iteration={$smarty.section.cu.iteration}
  index={$smarty.section.cu.index}
  id={$custid[cu]}<br />
{/section}
  
输出:
iteration=1 index=5 id=3005<br /> iteration=2 index=7 id=3007<br /> iteration=3 index=9 id=3009<br /> iteration=4 index=11 id=3011<br /> iteration=5 index=13 id=3013<br /> iteration=6 index=15 id=3015<br />
  这里是另一个例子,使用iteration属性来显示表格,
  并且每五行显示一次表头。
  
<table>
{section name=co loop=$contacts}
  {if $smarty.section.co.iteration is div by 5}
    <tr><th> </th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
  {/if}
  <tr>
    <td><a href="view.php?id={$contacts[co].id}">view<a></td>
    <td>{$contacts[co].name}</td>
    <td>{$contacts[co].home}</td>
    <td>{$contacts[co].cell}</td>
    <td>{$contacts[co].email}</td>
  <tr>
{/section}
</table>
  
     一个用iteration属性来交替显示文章每三行颜色的例子
    
  
  <table>
  {section name=co loop=$contacts}
    {if $smarty.section.co.iteration is even by 3}
      <span style="color: #ffffff">{$contacts[co].name}</span>
    {else}
      <span style="color: #dddddd">{$contacts[co].name}</span>
    {/if}
  {/section}
  </table>
  
    
  "is div by"语法是PHP取模运算的一个变种。取模运算{if $smarty.section.co.iteration % 5 == 1}也是可用的。
你还可以用"is odd by"来反转交替。
  如果当前的循环是最后一次,那么last将为 TRUE。
  
Example 7.73. {section} 属性 first 和 last
   例子循环了 $customers数组,在循环最前面输出头部区域,在底端输出底部区域的内容。
   同时也使用了
   total 属性.
   
{section name=customer loop=$customers}
  {if $smarty.section.customer.first}
    <table>
    <tr><th>id</th><th>customer</th></tr>
  {/if}
  <tr>
    <td>{$customers[customer].id}}</td>
    <td>{$customers[customer].name}</td>
  </tr>
  {if $smarty.section.customer.last}
    <tr><td></td><td>{$smarty.section.customer.total} customers</td></tr>
    </table>
  {/if}
{/section}
  
   rownum是当前循环的次数,从1开始。它是iteration
   的别名。
  
   loop 是最后一次{section}循环的下标。
   它可以在{section}循环中或者循环后使用。
     
Example 7.74. {section} 属性 loop
{section name=customer loop=$custid}
  {$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
There are {$smarty.section.customer.loop} customers shown above.
  
输出:
0 id: 1000<br /> 1 id: 1001<br /> 2 id: 1002<br /> There are 3 customers shown above.
   show是一个布尔值参数。如果设置为FALSE,section将不会被显示。
   如果有{sectionelse}显示,它们将被交替显示。
  
Example 7.75. show 属性 
布尔值 $show_customer_info 可以在PHP程序赋值并传递到模板中,
   可以控制section的显示与否。
{section name=customer loop=$customers show=$show_customer_info}
  {$smarty.section.customer.rownum} id: {$customers[customer]}<br />
{/section}
{if $smarty.section.customer.show}
  the section was shown.
{else}
  the section was not shown.
{/if}
  
输出:
1 id: 1000<br /> 2 id: 1001<br /> 3 id: 1002<br /> the section was shown.
   total是{section}的总数。
   它可以在{section}循环中或者循环后使用。
  
Example 7.76. total例子
{section name=customer loop=$custid step=2}
  {$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
   There are {$smarty.section.customer.total} customers shown above.
  
   参见{foreach},
   {for},
   {while}
    和
   $smarty.section.