Add missing documentation about for loop features

This commit is contained in:
Guillaume Gomez 2025-08-18 12:05:39 +02:00
parent 112a9b1552
commit acce36cede

View File

@ -466,7 +466,33 @@ Loop over each item in an iterator. For example:
<h1>Users</h1>
<ul>
{% for user in users %}
<li>{{ user.name|e }}</li>
<li>{{ user.name }}</li>
{% endfor %}
</ul>
```
You can filter items by adding an `if` condition:
```jinja
<h1>Users</h1>
<ul>
{% for user in users if user.is_activated %}
<li>{{ user.name }}</li>
{% endfor %}
</ul>
```
You can add an optional `{% else %}` block that is entered if the loop was never
entered, either because the iterator was empty, or the filter condition was never
match.
```jinja
<h1>Users</h1>
<ul>
{% for user in users %}
<li>{{ user.name }}</li>
{% else %}
<li>No users</li>
{% endfor %}
</ul>
```
@ -478,7 +504,6 @@ Inside for-loop blocks, some useful variables are accessible:
* *loop.first*: whether this is the first iteration of the loop
* *loop.last*: whether this is the last iteration of the loop
```jinja
<h1>Users</h1>
<ul>