<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<?php$data = array(1000,1001,1002);$smarty->assign('custid',$data);?>
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]}<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}
The above example will output:id: 1000<br />id: 1001<br />id: 1002<br /><hr />id: 1002<br />id: 1001<br />id: 1000<br /></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/><hr /><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/><hr /><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/> <?php<br/> $data = array(<br/> array('name' => 'John Smith', 'home' => '555-555-5555',<br/> 'cell' => '666-555-5555', 'email' => 'john@myexample.com'),<br/> array('name' => 'Jack Jones', 'home' => '777-555-5555',<br/> 'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),<br/> array('name' => 'Jane Munson', 'home' => '000-555-5555',<br/> 'cell' => '123456', 'email' => 'jane@myexample.com')<br/> );<br/> $smarty->assign('contacts',$data);<br/> ?><br/><br/> The template to output $contacts<br/> {section name=customer loop=$contacts}<br/> <p><br/> name: {$contacts[customer].name}<br /><br/> home: {$contacts[customer].home}<br /><br/> cell: {$contacts[customer].cell}<br /><br/> e-mail: {$contacts[customer].email}<br/> </p><br/> {/section}</p> <p> <br/> The above example will output:<br/> <p><br/>name: John Smith<br /><br/>home: 555-555-5555<br /><br/>cell: 666-555-5555<br /><br/>e-mail: john@myexample.com<br/></p><br/><p><br/>name: Jack Jones<br /><br/>home phone: 777-555-5555<br /><br/>cell phone: 888-555-5555<br /><br/>e-mail: jack@myexample.com<br/></p><br/><p><br/>name: Jane Munson<br /><br/>home phone: 000-555-5555<br /><br/>cell phone: 123456<br /><br/>e-mail: jane@myexample.com<br/></p></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/> <?php<br/> $id = array(1001,1002,1003);<br/> $smarty->assign('custid',$id);<br/> $fullnames = array('John Smith','Jack Jones','Jane Munson');<br/> $smarty->assign('name',$fullnames);<br/> $addr = array('253 Abbey road', '417 Mulberry ln', '5605 apple st');<br/> $smarty->assign('address',$addr);<br/> ?><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/> <p><br/>id: {$custid[customer]}<br /><br/>name: {$name[customer]}<br /><br/>address: {$address[customer]}<br/></p><br/>{/section}<br/><br/>The above example will output:<br/><p><br/>id: 1000<br /><br/>name: John Smith<br /><br/>address: 253 Abbey road<br/></p><br/><p><br/>id: 1001<br /><br/>name: Jack Jones<br /><br/>address: 417 Mulberry ln<br/></p><br/><p><br/>id: 1002<br /><br/>name: Jane Munson<br /><br/>address: 5605 apple st<br/></p></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/> <?php<br/> $id = array(1001,1002,1003);<br/> $smarty->assign('custid',$id);<br/> $fullnames = array('John Smith','Jack Jones','Jane Munson');<br/> $smarty->assign('name',$fullnames);<br/> $addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');<br/> $smarty->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->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->assign('contact_info', $info);<br/> ?><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/> <hr><br/> id: {$custid[customer]}<br /><br/> name: {$name[customer]}<br /><br/> address: {$address[customer]}<br /><br/> {section name=contact loop=$contact_type[customer]}<br/> {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br /><br/> {/section}<br/> {/section}<br/><br/> The above example will output:<br/> <hr><br/> id: 1000<br /><br/> name: John Smith<br /><br/> address: 253 N 45th<br /><br/> home phone: 555-555-5555<br /><br/> cell phone: 666-555-5555<br /><br/> e-mail: john@myexample.com<br /><br/> <hr><br/> id: 1001<br /><br/> name: Jack Jones<br /><br/> address: 417 Mulberry ln<br /><br/> home phone: 123-456-4<br /><br/> web: www.example.com<br /><br/> <hr><br/> id: 1002<br /><br/> name: Jane Munson<br /><br/> address: 5605 apple st<br /><br/> cell phone: 0457878<br /> </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/> <?php<br/> $sql = 'select id, name, home, cell, email from contacts '<br/>."where name like '$foo%' ";<br/>$smarty->assign('contacts', $db->getAll($sql));<br/>?><br/>The template to output the database result in a HTML table<br/><table><br/><tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr><br/>{section name=co loop=$contacts}<br/><tr><br/><td><a href="view.php?id={$contacts[co].id}">view<a></td><br/><td>{$contacts[co].name}</td><br/><td>{$contacts[co].home}</td><br/><td>{$contacts[co].cell}</td><br/><td>{$contacts[co].email}</td><br/><tr><br/>{sectionelse}<br/><tr><td colspan="5">No items found</td></tr><br/>{/section}<br/></table></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]}<br /><br/>{/section}<br/><br/>The above example will output:<br/>0 id: 1000<br /><br/>1 id: 1001<br /><br/>2 id: 1002<br /></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><?php<br/> $data = array(1001,1002,1003,1004,1005);<br/> $smarty->assign('rows',$data);<br/> ?><br/> Template to output the above array in a table<br/> {* $rows[row.index] and $rows[row] are identical in meaning *}<br/> <table><br/> <tr><br/> <th>index</th><th>id</th><br/> <th>index_prev</th><th>prev_id</th><br/> <th>index_next</th><th>next_id</th><br/> </tr><br/> {section name=row loop=$rows}<br/> <tr><br/> <td>{$smarty.section.row.index}</td><td>{$rows[row]}</td><br/> <td>{$smarty.section.row.index_prev}</td><td>{$rows[row.index_prev]}</td><br/> <td>{$smarty.section.row.index_next}</td><td>{$rows[row.index_next]}</td><br/> </tr><br/> {/section}<br/> </table></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><?php<br/> // array of 3000 to 3015<br/> $id = range(3000,3015);<br/> $smarty->assign('arr',$id);<br/> ?><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]}<br /><br/> {/section}<br/> The above example will output:<br/> iteration=1 index=5 id=3005<br /><br/> iteration=2 index=7 id=3007<br /><br/> iteration=3 index=9 id=3009<br /><br/> iteration=4 index=11 id=3011<br /><br/> iteration=5 index=13 id=3013<br /><br/> iteration=6 index=15 id=3015<br /></p> <p> Another example that uses the iteration property to output a table header block every five rows.<br/><br/><table><br/> {section name=co loop=$contacts}<br/> {if $smarty.section.co.iteration is div by 5}<br/><tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr><br/> {/if}<br/><tr><br/><td><a href="view.php?id={$contacts[co].id}">view<a></td><br/><td>{$contacts[co].name}</td><br/><td>{$contacts[co].home}</td><br/><td>{$contacts[co].cell}</td><br/><td>{$contacts[co].email}</td><br/><tr><br/> {/section}<br/></table><br/> An that uses the iteration property to alternate a text color every third row.<br/><br/><table><br/> {section name=co loop=$contacts}<br/> {if $smarty.section.co.iteration is even by 3}<br/><span style="color: #ffffff">{$contacts[co].name}</span><br/> {else}<br/><span style="color: #dddddd">{$contacts[co].name}</span><br/> {/if}<br/> {/section}<br/></table><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/><table><br/><tr><th>id</th><th>customer</th></tr><br/>{/if}<br/><tr><br/><td>{$customers[customer].id}}</td><br/><td>{$customers[customer].name}</td><br/></tr><br/>{if $smarty.section.customer.last}<br/><tr><td></td><td>{$smarty.section.customer.total} customers</td></tr><br/></table><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]}<br /><br/> {/section}<br/> There are {$smarty.section.customer.loop} customers shown above.<br/><br/> The above example will output:<br/> 0 id: 1000<br /><br/> 1 id: 1001<br /><br/> 2 id: 1002<br /><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]}<br /><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<br /><br/>2 id: 1001<br /><br/>3 id: 1002<br /><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]}<br /><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>
- Smarty模板編譯引擎
- 序
- 譯序
- I.開始
- 第一章. 什么是Smarty?
- 第二章. 安裝
- II.模板設計者篇
- 第三章.基本語法
- 注釋
- 變量
- 函數
- 屬性
- 雙引號里嵌入變量
- 數學運算
- 忽略Smarty解析
- 第四章.變量
- 從PHP分配的變量
- 從配置文件讀取的變量
- 變量范圍
- {$smarty}保留變量
- 第五章.變量調節器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.組合修改器
- 第七章.內置函數
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定義函數
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.調試控制臺
- III.模板程序員篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty類變量
- $template_dir
- 第十三章.Smarty類方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.緩存
- 建立緩存
- 多重緩存
- 緩存集合
- 控制插件輸出的可緩存性
- 第十五章.高級特性
- 安全
- 通過模板更改設置
- 模板繼承
- 數據流
- 對象
- 靜態類
- 預過濾器
- 后過濾器
- 輸出過濾器
- 緩存處理函數
- 資源
- 第十六章.以插件擴展Smarty
- 插件如何工作
- 命名約定
- 編寫插件
- 模板函數
- 調節器
- 塊函數
- 編譯函數
- 預濾器/后濾器
- 輸出過濾器
- 資源
- 插入
- Ⅳ.附錄
- 第十七章.疑難解答
- Smarty/PHP 錯誤
- 第十八章.使用技巧和經驗
- 空白變量處理
- 默認變量處理
- 傳遞變量標題給頭模板
- 日期
- WAP/WML
- 組件化模板
- 拒絕電子郵件地址
- 第十九章. 相關資源
- 第二十章. 漏洞
- 3.0安裝包
- 2.x版本升級至3.x版本的提示
- 3.0.x使用指南
- 翻譯人員列表