ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# `for` Loop over each item in a sequence. For example, to display a list of usersprovided in a variable called `users`: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5 6</pre></div></td><td class="code"><div class="highlight"><pre>&lt;h1&gt;Members&lt;/h1&gt; &lt;ul&gt; {% for user in users %} &lt;li&gt;{{ user.username|e }}&lt;/li&gt; {% endfor %} &lt;/ul&gt; </pre></div></td></tr></table> Note A sequence can be either an array or an object implementing the`Traversable` interface. If you do need to iterate over a sequence of numbers, you can use the `..`operator: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3</pre></div></td><td class="code"><div class="highlight"><pre>{% for i in 0..10 %} * {{ i }} {% endfor %} </pre></div></td></tr></table> The above snippet of code would print all numbers from 0 to 10. It can be also useful with letters: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3</pre></div></td><td class="code"><div class="highlight"><pre>{% for letter in 'a'..'z' %} * {{ letter }} {% endfor %} </pre></div></td></tr></table> The `..` operator can take any expression at both sides: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3</pre></div></td><td class="code"><div class="highlight"><pre>{% for letter in 'a'|upper..'z'|upper %} * {{ letter }} {% endfor %} </pre></div></td></tr></table> ### The loop variable Inside of a `for` loop block you can access some special variables: | Variable | Description | |-----|-----| | `loop.index` | The current iteration of the loop. (1 indexed) | | `loop.index0` | The current iteration of the loop. (0 indexed) | | `loop.revindex` | The number of iterations from the end of the loop (1 indexed) | | `loop.revindex0` | The number of iterations from the end of the loop (0 indexed) | | `loop.first` | True if first iteration | | `loop.last` | True if last iteration | | `loop.length` | The number of items in the sequence | | `loop.parent` | The parent context | <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3</pre></div></td><td class="code"><div class="highlight"><pre>{% for user in users %} {{ loop.index }} - {{ user.username }} {% endfor %} </pre></div></td></tr></table> Note The `loop.length`, `loop.revindex`, `loop.revindex0`, and`loop.last` variables are only available for PHP arrays, or objects thatimplement the `Countable` interface. They are also not available whenlooping with a condition. New in version 1.2: The `if` modifier support has been added in Twig 1.2. ### Adding a condition Unlike in PHP, it's not possible to `break` or `continue` in a loop. Youcan however filter the sequence during iteration which allows you to skipitems. The following example skips all the users which are not active: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5</pre></div></td><td class="code"><div class="highlight"><pre>&lt;ul&gt; {% for user in users if user.active %} &lt;li&gt;{{ user.username|e }}&lt;/li&gt; {% endfor %} &lt;/ul&gt; </pre></div></td></tr></table> The advantage is that the special loop variable will count correctly thus notcounting the users not iterated over. Keep in mind that properties like`loop.last` will not be defined when using loop conditions. Note Using the `loop` variable within the condition is not recommended as itwill probably not be doing what you expect it to. For instance, adding acondition like `loop.index > 4` won't work as the index is onlyincremented when the condition is true (so the condition will nevermatch). ### The else Clause If no iteration took place because the sequence was empty, you can render areplacement block by using `else`: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5 6 7</pre></div></td><td class="code"><div class="highlight"><pre>&lt;ul&gt; {% for user in users %} &lt;li&gt;{{ user.username|e }}&lt;/li&gt; {% else %} &lt;li&gt;&lt;em&gt;no user found&lt;/em&gt;&lt;/li&gt; {% endfor %} &lt;/ul&gt; </pre></div></td></tr></table> ### Iterating over Keys By default, a loop iterates over the values of the sequence. You can iterateon keys by using the `keys` filter: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5 6</pre></div></td><td class="code"><div class="highlight"><pre>&lt;h1&gt;Members&lt;/h1&gt; &lt;ul&gt; {% for key in users|keys %} &lt;li&gt;{{ key }}&lt;/li&gt; {% endfor %} &lt;/ul&gt; </pre></div></td></tr></table> ### Iterating over Keys and Values You can also access both keys and values: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5 6</pre></div></td><td class="code"><div class="highlight"><pre>&lt;h1&gt;Members&lt;/h1&gt; &lt;ul&gt; {% for key, user in users %} &lt;li&gt;{{ key }}: {{ user.username|e }}&lt;/li&gt; {% endfor %} &lt;/ul&gt; </pre></div></td></tr></table> ### Iterating over a Subset You might want to iterate over a subset of values. This can be achieved usingthe [*slice*](#) filter: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5 6</pre></div></td><td class="code"><div class="highlight"><pre>&lt;h1&gt;Top Ten Members&lt;/h1&gt; &lt;ul&gt; {% for user in users|slice(0, 10) %} &lt;li&gt;{{ user.username|e }}&lt;/li&gt; {% endfor %} &lt;/ul&gt; </pre></div></td></tr></table>