<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.extends.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.foreach.html" accesskey="N">Next</a></td></tr></table> # {for} 循环 The {for}{forelse} tag is used to create simple loops. The following different formarts are supported: * {for $var=$start to $end} simple loop with step size of 1. * {for $var=$start to $end step $step} loop with individual step size. {forelse} is executed when the loop is not iterated. {for}、{forelse}标签用来创建一个简单循环,支持以下不同的格式: {for $var=$start to $end}步长为1的简单循环; {for $var=$start to $end step $step}其它步长循环。 当循环无迭代时执行{forelse}。 **Attribute: ** | Attribute Name | Shorthand | Type | Required | Default | Description | |-----|-----|-----|-----|-----|-----| | max | n/a | integer | no | *n/a* | Limit the number of iterations | **Option Flags:** | **Name** | **Description** | |-----|-----| | nocache | Disables caching of the {for} loop | **属性: ** | 属性名 | 简写 | 类型 | 是否必须 | 缺省值 | 描述 | |-----|-----|-----|-----|-----|-----| | max | n/a | int | no | *n/a* | 迭代的最大次数 | **选项标记:** | **名称** | **描述** | |-----|-----| | nocache | 禁止{for}循环缓存 | <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN727" id="AEN727"> </a> <b><span class="PROGRAMLISTING">Example 7.27. A simple {for} loop</span><br/> 例 7-27.简单的{for}</b><strong>循环</strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&lt;ul&gt;{for $foo=1 to 3} &lt;li&gt;{$foo}&lt;/li&gt;{/for}&lt;/ul&gt; The above example will output:&lt;ul&gt; &lt;li&gt;1&lt;/li&gt; &lt;li&gt;2&lt;/li&gt; &lt;li&gt;3&lt;/li&gt;&lt;/ul&gt;</pre></td> </tr></table><p><strong><span class="PROGRAMLISTING">Example 7.28. Using the max attribute</span><br/> 例 7-28.使用最大属性</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&lt;?php$smarty-&gt;assign('to',10);?&gt;&#13; &lt;ul&gt;{for $foo=3 to $to max=3} &lt;li&gt;{$foo}&lt;/li&gt;{/for}&lt;/ul&gt; The above example will output:&lt;ul&gt; &lt;li&gt;3&lt;/li&gt; &lt;li&gt;4&lt;/li&gt; &lt;li&gt;5&lt;/li&gt;&lt;/ul&gt;</pre></td> </tr></table><p><strong><span class="PROGRAMLISTING">Example 7.29. Excution of {forelse}</span><br/>例 7-28.执行{forelse}</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">$smarty-&gt;assign('start',10);$smarty-&gt;assign('to',5); &lt;ul&gt;{for $foo=$start to $to} &lt;li&gt;{$foo}&lt;/li&gt;{forelse} no iteration{/for}&lt;/ul&gt; The above example will output: no iteration&#13; </pre></td> </tr></table><p><span class="PROGRAMLISTING">See also {foreach} and {section}</span><br/> 参考<a href="language.function.foreach.html">{foeach}</a>与<a href="language.function.section.html">{section}</a>。<br/></p> </div></td> </tr></table> <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.extends.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.foreach.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">{extends}<br/> 继承</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">{foreach},{foreachelse}</td></tr></table>