<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="center">Smarty - the compiling PHP template engine</th></tr><tr><td width="25%" align="left" valign="bottom"><a href="language.function.php.html" accesskey="P">Prev</a></td> <td width="50%" align="center" valign="bottom">Chapter 7. Built-in Functions[第七章.内建函数]</td> <td width="25%" align="right" valign="bottom"><a href="language.function.while.html" accesskey="N">Next</a></td></tr></table> # {section},{sectionelse}遍历数组 **Table of Contents目录**[.index](#)[.index_prev](#)[.index_next](#)[.iteration](#)[.first](#)[.last](#)[.rownum](#)[.loop](#)[.show](#)[.total](#) A {section} is for looping over sequentially indexed arrays of data, unlike {foreach} which is used to loop over a single associative array. Every {section} tag must be paired with a closing {/section} tag. 不同于{foreach}遍历单层关联数组,{section}支持循序索引遍历数组中的数据(支持一次性读取多维数组)。每个{section}标签必须与闭合标签{/section}成对出现。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> The {foreach} loop can do everything a {section} loop can do, and has a simpler and easier syntax. It is usually preferred over the {section} loop.<br/> {foreach}可以做{section}能做的所有事,而且语法更简单、更容易。它通常是循环数组的首选。</td> </tr></table> <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> {section} loops cannot loop over associative arrays, they must be numerically indexed, and sequential (0,1,2,...). For associative arrays, use the {foreach} loop. <br/> {section}循环不能遍历关联数组,(被循环的)数组必须是数字索引,像这样(0,1,2,...)。对于关联数组,请用{foreach}循环。</td> </tr></table> **Attribute:** | Attribute Name | Type | Required | Default | Description | |-----|-----|-----|-----|-----| | name | string | Yes | *n/a* | The name of the section | | loop | [$variable_name] | Yes | *n/a* | The name of the variable to determine # of loop iterations | | start | integer | No | *0* | The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value. | | step | integer | No | *1* | The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is negative, it will step through the array backwards. | | max | integer | No | *1* | Sets the maximum number of times the section will loop. | | show | boolean | No | *true* | determines whether or not to show this section | **Option Flags:** | **Name** | **Description** | |-----|-----| | nocache | Disables caching of the {section} loop | **属性:** | 属性 | 类型 | 是否必须 | 缺省值 | 描述 | |-----|-----|-----|-----|-----| | name | string | Yes | *n/a* | 该循环的名称 | | loop | [$variable_name] | Yes | *n/a* | 决定循环次数的变量名称 | | start | integer | No | *0* | 循环执行的初始位置. 如果该值为负数,开始位置从数组的尾部算起. 例如:如果数组中有7个元素,指定start为-2,那么指向当前数组的索引为5. 非法值(超过了循环数组的下限)将被自动调整为最接近的合法值. | | step | integer | No | *1* | 该值决定循环的步长. 例如指定step=2将只遍历下标为0、2、4等的元素. 如果step为负值,那么遍历数组的时候从后向前遍历. | | max | integer | No | *1* | 设定循环最大执行次数. | | show | boolean | No | *true* | 决定是否显示该循环. | **选项标记:** | **名称** | **描述** | |-----|-----| | nocache | {section} 循环禁用缓存 | * Required attributes are name and loop. * The name of the {section} can be anything you like, made up of letters, numbers and underscores,like PHP variables [http://php.net/language.variables]. * {section}'s can be nested, and the nested {section} names must be unique from each other. * The loop attribute, usually an array of values, determines the number of times the {section} will loop. You can also pass an integer as the loop value. * When printing a variable within a {section}, the {section} name must be given next to variable name within [brackets]. * {sectionelse} is executed when there are no values in the loop variable. * A {section} also has its own variables that handle {section} properties. These properties are accessible as: {$smarty.section.name.property} where “name” is the attribute name. * {section} properties are index, index_prev, index_next, iteration, first, last,rownum, loop, show, total. 必须设置*name*和*loop*属性; {section}的名称可以是你喜欢的任何字母、下划线、数字组合,与php变量(命名)相同; {section}可以嵌套,但必须确保嵌套的{section}名称唯一; *loop*属性通常是一个数组值,它决定{section}循环的次数。你也可以为loop提供一个具体的整数值; 当需要在{section}内输出一个变量时,{section}的变量名必须用方括号括起并挨在该输出变量名的右边(译注:如$abc[mysection]); 当循环变量无值时执行{sectionelse}; {section}同样有自己的变量处理{section}属性,可以通过调用{$smarty.section.name.property}访问这些属性,这里‘name’是个变量名; {section}的属性有index、index_prev、index_next、iteration、first、last、rownum、loop、show、total。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN2760" id="AEN2760"/> <b><span class="PROGRAMLISTING">Example 7.60. Looping a simple array with {section}</span><br/> 例 7-60. 一个简单{section}循环演示 </b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">assign() an array to Smarty&lt;?php$data = array(1000,1001,1002);$smarty-&gt;assign('custid',$data);?&gt;&#13; &#13; &#13; The template that outputs the array{* 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]}&lt;br /&gt;{/section}&lt;hr /&gt;{* 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]}&lt;br /&gt;{/section}&#13; &#13; &#13; The above example will output:id: 1000&lt;br /&gt;id: 1001&lt;br /&gt;id: 1002&lt;br /&gt;&lt;hr /&gt;id: 1002&lt;br /&gt;id: 1001&lt;br /&gt;id: 1000&lt;br /&gt;</pre></td></tr></table></div></td></tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <p><b>Example 7.61. {section} without an assigned array<br/> 例 7-61.未分配数组的{section}演示 </b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> {section name=foo start=10 loop=20 step=2}<br/>{$smarty.section.foo.index}<br/>{/section}<br/>&lt;hr /&gt;<br/>{section name=bar loop=21 max=6 step=-2}<br/>{$smarty.section.bar.index}<br/>{/section}<br/>The above example will output:<br/><br/>10 12 14 16 18<br/>&lt;hr /&gt;<br/>20 18 16 14 12 10</td></tr></table></div></td></tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <p><b>Example 7.62. Naming a {section}<br/> 例 7-62. {section}名称演示</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> The name of the {section} can be anything you like, see PHP variables [http://php.net/ language.variables]. It is used to reference the data within the {section}.<br/> {section name=anything loop=$myArray}<br/>{$myArray[anything].foo}<br/>{$name[anything]}<br/>{$address[anything].bar}<br/>{/section}</td></tr></table></div></td></tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <p><b>Example 7.63. Looping an associative array with {section}<br/> 例 7-63. 用{section}循环关联数组的演示</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <p>This is an example of printing an associative array of data with a {section}. Following is the php script to assign the $contacts array to Smarty.<br/><br/> &lt;?php<br/> $data = array(<br/> array('name' =&gt; 'John Smith', 'home' =&gt; '555-555-5555',<br/> 'cell' =&gt; '666-555-5555', 'email' =&gt; 'john@myexample.com'),<br/> array('name' =&gt; 'Jack Jones', 'home' =&gt; '777-555-5555',<br/> 'cell' =&gt; '888-555-5555', 'email' =&gt; 'jack@myexample.com'),<br/> array('name' =&gt; 'Jane Munson', 'home' =&gt; '000-555-5555',<br/> 'cell' =&gt; '123456', 'email' =&gt; 'jane@myexample.com')<br/> );<br/> $smarty-&gt;assign('contacts',$data);<br/> ?&gt;<br/><br/> The template to output $contacts<br/> {section name=customer loop=$contacts}<br/> &lt;p&gt;<br/> name: {$contacts[customer].name}&lt;br /&gt;<br/> home: {$contacts[customer].home}&lt;br /&gt;<br/> cell: {$contacts[customer].cell}&lt;br /&gt;<br/> e-mail: {$contacts[customer].email}<br/> &lt;/p&gt;<br/> {/section}</p> <p> <br/> The above example will output:<br/> &lt;p&gt;<br/>name: John Smith&lt;br /&gt;<br/>home: 555-555-5555&lt;br /&gt;<br/>cell: 666-555-5555&lt;br /&gt;<br/>e-mail: john@myexample.com<br/>&lt;/p&gt;<br/>&lt;p&gt;<br/>name: Jack Jones&lt;br /&gt;<br/>home phone: 777-555-5555&lt;br /&gt;<br/>cell phone: 888-555-5555&lt;br /&gt;<br/>e-mail: jack@myexample.com<br/>&lt;/p&gt;<br/>&lt;p&gt;<br/>name: Jane Munson&lt;br /&gt;<br/>home phone: 000-555-5555&lt;br /&gt;<br/>cell phone: 123456&lt;br /&gt;<br/>e-mail: jane@myexample.com<br/>&lt;/p&gt;</p></td></tr></table></div></td></tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <p><b>Example 7.64. {section} demonstrating the loop variable<br/> 例 7-64. 演示{section}循环变量</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <p>This example assumes假设 that $custid, $name and $address are all arrays containing the same number个数 of values. First the php script that assign's the arrays to Smarty.<br/> &lt;?php<br/> $id = array(1001,1002,1003);<br/> $smarty-&gt;assign('custid',$id);<br/> $fullnames = array('John Smith','Jack Jones','Jane Munson');<br/> $smarty-&gt;assign('name',$fullnames);<br/> $addr = array('253 Abbey road', '417 Mulberry ln', '5605 apple st');<br/> $smarty-&gt;assign('address',$addr);<br/> ?&gt;<br/> The loop variable only determines the number of times to loop. You can access ANY variable from the template within the {section}. This is useful for looping multiple arrays. You can pass an array which will determine the loop count by the array size, or you can pass an integer to specify the number of loops.</p> <p>loop变量只决定循环的次数,你可以访问模版{section}中的任何变量,这个特性在多维数组中非常有用。你可以传递一个数组,该数组的大小决定循环间隔,或者您也可以通过一个整数来指定循环次数。</p> <p>{section name=customer loop=$custid}<br/> &lt;p&gt;<br/>id: {$custid[customer]}&lt;br /&gt;<br/>name: {$name[customer]}&lt;br /&gt;<br/>address: {$address[customer]}<br/>&lt;/p&gt;<br/>{/section}<br/><br/>The above example will output:<br/>&lt;p&gt;<br/>id: 1000&lt;br /&gt;<br/>name: John Smith&lt;br /&gt;<br/>address: 253 Abbey road<br/>&lt;/p&gt;<br/>&lt;p&gt;<br/>id: 1001&lt;br /&gt;<br/>name: Jack Jones&lt;br /&gt;<br/>address: 417 Mulberry ln<br/>&lt;/p&gt;<br/>&lt;p&gt;<br/>id: 1002&lt;br /&gt;<br/>name: Jane Munson&lt;br /&gt;<br/>address: 5605 apple st<br/>&lt;/p&gt;</p></td></tr></table></div></td></tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <p><b>Example 7.65. Nested {section}'s<br/> 例 7-65. {section}嵌套 演示</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <p>{section}'s can be nested as deep as you like. With nested {section}'s, you can access complex data structures, such as multi-dimensional arrays. This is an example .php script thats assign's the arrays.</p> <p>{section}可以无限嵌套,在嵌套{section}里,你可以访问复杂的数据结构,如多维数组。<br/> &lt;?php<br/> $id = array(1001,1002,1003);<br/> $smarty-&gt;assign('custid',$id);<br/> $fullnames = array('John Smith','Jack Jones','Jane Munson');<br/> $smarty-&gt;assign('name',$fullnames);<br/> $addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');<br/> $smarty-&gt;assign('address',$addr);<br/> $types = array(<br/> array( 'home phone', 'cell phone', 'e-mail'),<br/> array( 'home phone', 'web'),<br/> array( 'cell phone')<br/> );<br/> $smarty-&gt;assign('contact_type', $types);<br/> $info = array(<br/> array('555-555-5555', '666-555-5555', 'john@myexample.com'),<br/> array( '123-456-4', 'www.example.com'),<br/> array( '0457878')<br/> );<br/> $smarty-&gt;assign('contact_info', $info);<br/> ?&gt;<br/><br/> In this template, $contact_type[customer] is an array of contact types for the current customer.<br/> 在这个例子里面,$contact_type[customer]是当前customer的连联类型数组。<br/><br/> {section name=customer loop=$custid}<br/> &lt;hr&gt;<br/> id: {$custid[customer]}&lt;br /&gt;<br/> name: {$name[customer]}&lt;br /&gt;<br/> address: {$address[customer]}&lt;br /&gt;<br/> {section name=contact loop=$contact_type[customer]}<br/> {$contact_type[customer][contact]}: {$contact_info[customer][contact]}&lt;br /&gt;<br/> {/section}<br/> {/section}<br/><br/> The above example will output:<br/> &lt;hr&gt;<br/> id: 1000&lt;br /&gt;<br/> name: John Smith&lt;br /&gt;<br/> address: 253 N 45th&lt;br /&gt;<br/> home phone: 555-555-5555&lt;br /&gt;<br/> cell phone: 666-555-5555&lt;br /&gt;<br/> e-mail: john@myexample.com&lt;br /&gt;<br/> &lt;hr&gt;<br/> id: 1001&lt;br /&gt;<br/> name: Jack Jones&lt;br /&gt;<br/> address: 417 Mulberry ln&lt;br /&gt;<br/> home phone: 123-456-4&lt;br /&gt;<br/> web: www.example.com&lt;br /&gt;<br/> &lt;hr&gt;<br/> id: 1002&lt;br /&gt;<br/> name: Jane Munson&lt;br /&gt;<br/> address: 5605 apple st&lt;br /&gt;<br/> cell phone: 0457878&lt;br /&gt; </p> </td></tr></table></div></td></tr></table> **Example 7.66. Database example with a {sectionelse} 例 7-66. {sectionelse}数据库演示** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> Results of a database search (eg ADODB or PEAR) are assigned to Smarty<br/><br/> &lt;?php<br/> $sql = 'select id, name, home, cell, email from contacts '<br/>."where name like '$foo%' ";<br/>$smarty-&gt;assign('contacts', $db-&gt;getAll($sql));<br/>?&gt;<br/>The template to output the database result in a HTML table<br/>&lt;table&gt;<br/>&lt;tr&gt;&lt;th&gt;&amp;nbsp;&lt;/th&gt;&lt;th&gt;Name&gt;&lt;/th&gt;&lt;th&gt;Home&lt;/th&gt;&lt;th&gt;Cell&lt;/th&gt;&lt;th&gt;Email&lt;/th&gt;&lt;/tr&gt;<br/>{section name=co loop=$contacts}<br/>&lt;tr&gt;<br/>&lt;td&gt;&lt;a href="view.php?id={$contacts[co].id}"&gt;view&lt;a&gt;&lt;/td&gt;<br/>&lt;td&gt;{$contacts[co].name}&lt;/td&gt;<br/>&lt;td&gt;{$contacts[co].home}&lt;/td&gt;<br/>&lt;td&gt;{$contacts[co].cell}&lt;/td&gt;<br/>&lt;td&gt;{$contacts[co].email}&lt;/td&gt;<br/>&lt;tr&gt;<br/>{sectionelse}<br/>&lt;tr&gt;&lt;td colspan="5"&gt;No items found&lt;/td&gt;&lt;/tr&gt;<br/>{/section}<br/>&lt;/table&gt;</td> </tr></table> .index index contains the current array index, starting with zero or the start attribute if given. It increments by one or by the step attribute if given. index包含当前数组的索引,以0开始,如果设置了start变量(属性)则以该数字开始。{section}循环一次索引自动向前1步,如果给出了step变量则以该步长向前。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/>If the step and start properties are not modified, then this works the same as the iteration property, except it starts at zero instead of one.<br/>如果没有修改step和start变量,则效果与iteration属性(见下文的<a href="#iteration">.iteration</a>)一样,除非后者以1开始,而不是0。</td> </tr></table> **Example 7.67. {section} index property 例 7-67. {section}的index属性(变量)演示** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> Note<br/> $custid[customer.index] and $custid[customer] are identical相同的.<br/> {section name=customer loop=$custid}<br/>{$smarty.section.customer.index} id: {$custid[customer]}&lt;br /&gt;<br/>{/section}<br/><br/>The above example will output:<br/>0 id: 1000&lt;br /&gt;<br/>1 id: 1001&lt;br /&gt;<br/>2 id: 1002&lt;br /&gt;</td> </tr></table> .index_prev index_prev is the previous loop index. On the first loop, this is set to -1. index_prev为前一循环的索引值。 循环开始时,此值为-1。 .index_next index_next is the next loop index. On the last loop, this is still one more than the current index,respecting the setting of the step attribute, if given. index_next为下个循环的索引值。循环到最尾,此值仍然比当前索引值大1,如果设定了step,取决于该值。 **Example 7.68. index, index_next and index_prev properties 例 7-68. index、index_next和index_prev属性演示** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;?php<br/> $data = array(1001,1002,1003,1004,1005);<br/> $smarty-&gt;assign('rows',$data);<br/> ?&gt;<br/> Template to output the above array in a table<br/> {* $rows[row.index] and $rows[row] are identical in meaning *}<br/> &lt;table&gt;<br/> &lt;tr&gt;<br/> &lt;th&gt;index&lt;/th&gt;&lt;th&gt;id&lt;/th&gt;<br/> &lt;th&gt;index_prev&lt;/th&gt;&lt;th&gt;prev_id&lt;/th&gt;<br/> &lt;th&gt;index_next&lt;/th&gt;&lt;th&gt;next_id&lt;/th&gt;<br/> &lt;/tr&gt;<br/> {section name=row loop=$rows}<br/> &lt;tr&gt;<br/> &lt;td&gt;{$smarty.section.row.index}&lt;/td&gt;&lt;td&gt;{$rows[row]}&lt;/td&gt;<br/> &lt;td&gt;{$smarty.section.row.index_prev}&lt;/td&gt;&lt;td&gt;{$rows[row.index_prev]}&lt;/td&gt;<br/> &lt;td&gt;{$smarty.section.row.index_next}&lt;/td&gt;&lt;td&gt;{$rows[row.index_next]}&lt;/td&gt;<br/> &lt;/tr&gt;<br/> {/section}<br/> &lt;/table&gt;</p> <p>The above example will output a table containing the following:<br/> index id index_prev prev_id index_next next_id<br/> 0 1001 -1 1 1002<br/> 1 1002 0 1001 2 1003<br/> 2 1003 1 1002 3 1004<br/> 3 1004 2 1003 4 1005<br/> 4 1005 3 1004 5</p></td> </tr></table> .iteration iteration contains the current loop iteration and starts at one. iteration包含当前的迭代循环,开始值为1。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/>This is not affected by the {section} properties start, step and max, unlike the index property. iteration also starts with one instead of zero unlike index. rownum is an alias to iteration, they are identical.<br/><p>.iteration不像.index属性受start、step和max属性的影响,该值总是从1开始(index是从0开始的)。.rownum是.iteration的别名,两者意思一样。</p><p> </p></td> </tr></table> 译注 | 本属性用来统计循环迭代多少次。 | |-----| **Example 7.69. A section's iteration property 例 7-69. {section}的iteration属性演示** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;?php<br/> // array of 3000 to 3015<br/> $id = range(3000,3015);<br/> $smarty-&gt;assign('arr',$id);<br/> ?&gt;<br/> Template to output every other element of the $arr array as step=2<br/> {section name=cu loop=$arr start=5 step=2}<br/> iteration={$smarty.section.cu.iteration}<br/> index={$smarty.section.cu.index}<br/> id={$custid[cu]}&lt;br /&gt;<br/> {/section}<br/> The above example will output:<br/> iteration=1 index=5 id=3005&lt;br /&gt;<br/> iteration=2 index=7 id=3007&lt;br /&gt;<br/> iteration=3 index=9 id=3009&lt;br /&gt;<br/> iteration=4 index=11 id=3011&lt;br /&gt;<br/> iteration=5 index=13 id=3013&lt;br /&gt;<br/> iteration=6 index=15 id=3015&lt;br /&gt;</p> <p> Another example that uses the iteration property to output a table header block every five rows.<br/><br/>&lt;table&gt;<br/> {section name=co loop=$contacts}<br/> {if $smarty.section.co.iteration is div by 5}<br/>&lt;tr&gt;&lt;th&gt;&amp;nbsp;&lt;/th&gt;&lt;th&gt;Name&gt;&lt;/th&gt;&lt;th&gt;Home&lt;/th&gt;&lt;th&gt;Cell&lt;/th&gt;&lt;th&gt;Email&lt;/th&gt;&lt;/tr&gt;<br/> {/if}<br/>&lt;tr&gt;<br/>&lt;td&gt;&lt;a href="view.php?id={$contacts[co].id}"&gt;view&lt;a&gt;&lt;/td&gt;<br/>&lt;td&gt;{$contacts[co].name}&lt;/td&gt;<br/>&lt;td&gt;{$contacts[co].home}&lt;/td&gt;<br/>&lt;td&gt;{$contacts[co].cell}&lt;/td&gt;<br/>&lt;td&gt;{$contacts[co].email}&lt;/td&gt;<br/>&lt;tr&gt;<br/> {/section}<br/>&lt;/table&gt;<br/> An that uses the iteration property to alternate a text color every third row.<br/><br/>&lt;table&gt;<br/> {section name=co loop=$contacts}<br/> {if $smarty.section.co.iteration is even by 3}<br/>&lt;span style="color: #ffffff"&gt;{$contacts[co].name}&lt;/span&gt;<br/> {else}<br/>&lt;span style="color: #dddddd"&gt;{$contacts[co].name}&lt;/span&gt;<br/> {/if}<br/> {/section}<br/>&lt;/table&gt;<br/></p> </td> </tr></table> <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/>The "is div by" syntax is a simpler alternative to the PHP mod operator syntax. The mod operator is allowed: {if $smarty.section.co.iteration % 5 == 1} will work just the same.<br/>is div by'语法是一个php模式语法的简单替代,在.iteration模式中允许:{if $smarty.section.co.iteration % 5 == 1} ,在效果上与前者是一样的。<p/></td> </tr></table> <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td><br/>Note<br/>You can also use "is odd by" to reverse the alternating. <br/>你也可以用'is odd by'倒序交替。</td> </tr></table>   .first first is set to TRUE if the current {section} iteration is the initial one. 如果当前循环第一次执行,first被设置为true。 .last last is set to TRUE if the current section iteration is the final one. 如果当前section迭代执行到最尾,last 被设置为true。 **Example 7.70. {section} property first and last 例 7-70. {section}的first和last属性演示** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> This example loops the $customers array, outputs a header block on the first iteration and on the last<br/> outputs the footer block. Also uses the total property.<br/> {section name=customer loop=$customers}<br/>{if $smarty.section.customer.first}<br/>&lt;table&gt;<br/>&lt;tr&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;customer&lt;/th&gt;&lt;/tr&gt;<br/>{/if}<br/>&lt;tr&gt;<br/>&lt;td&gt;{$customers[customer].id}}&lt;/td&gt;<br/>&lt;td&gt;{$customers[customer].name}&lt;/td&gt;<br/>&lt;/tr&gt;<br/>{if $smarty.section.customer.last}<br/>&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;{$smarty.section.customer.total} customers&lt;/td&gt;&lt;/tr&gt;<br/>&lt;/table&gt;<br/>{/if}<br/>{/section}</td> </tr></table>   .rownum rownum contains the current loop iteration, starting with one. It is an alias to iteration, they work identically. rownum包含当前循环迭代。从1开始。该属性是iteration的别名,两者是一样的。   .loop loop contains the last index number that this {section} looped. This can be used inside or after the {section}. loop包含{section}上次循环时的最后索引值, 该值可以用于循环内部或循环结束后。 **Example 7.71. {section} property loop 例 7-71. {section}loop属性演示** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> {section name=customer loop=$custid}<br/> {$smarty.section.customer.index} id: {$custid[customer]}&lt;br /&gt;<br/> {/section}<br/> There are {$smarty.section.customer.loop} customers shown above.<br/><br/> The above example will output:<br/> 0 id: 1000&lt;br /&gt;<br/> 1 id: 1001&lt;br /&gt;<br/> 2 id: 1002&lt;br /&gt;<br/> There are 3 customers shown above.</p> <p/></td> </tr></table> .show show is used as a parameter to section and is a boolean value. If FALSE, the section will not be displayed. If there is a {sectionelse} present, that will be alternately displayed. *show*是section的参数,为布尔值,如果设置为 false,该循环将不显示。如果存在{sectionelse}子句,该字句将交替显示。 **Example 7.72. show property 例 7-72. {section}show属性演示** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> Boolean $show_customer_info has been passed from the PHP application, to regulate whether or not this section shows.<br/> {section name=customer loop=$customers show=$show_customer_info}<br/>{$smarty.section.customer.rownum} id: {$customers[customer]}&lt;br /&gt;<br/>{/section}<br/>{if $smarty.section.customer.show}<br/>the section was shown.<br/>{else}<br/>the section was not shown.<br/>{/if}<br/><br/>The above example will output:<br/>1 id: 1000&lt;br /&gt;<br/>2 id: 1001&lt;br /&gt;<br/>3 id: 1002&lt;br /&gt;<br/>the section was shown.</td> </tr></table> .total total contains the number of iterations that this {section} will loop. This can be used inside or after a {section}. total包含{section}循环执行总的次数,可以在{section}执行中/后调用此属性。 **Example 7.73. total property example 例 7-73. total属性演示** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> {section name=customer loop=$custid step=2}<br/>{$smarty.section.customer.index} id: {$custid[customer]}&lt;br /&gt;<br/>{/section}<br/>There are {$smarty.section.customer.total} customers shown above.<br/></td> </tr></table> 参见[{foreach}](#)、[{for}](#)和[$smarty.section](#)。 <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="language.function.php.html" accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="language.function.while.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">{php}</td><td width="34%" align="center" valign="top"><a href="language.builtin.functions.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">{while}<br/> 循环</td></tr></table>