Jinja Special Loop Variables
n
nn
How It Works
nEach time the `for` loop iterates over an item, the `loop` object is updated with the current status of the loop. For example, `loop.index` tells you the current iteration number, which is useful for creating numbered lists. The `loop.first` and `loop.last` variables are booleans that allow you to apply special styling or logic to the first and last items in a sequence.nnThese variables help make your templates more flexible and dynamic without needing complex logic in the view or controller. You can access them simply by referencing the `loop` object followed by the variable name (e.g., `loop.index`).n
n
List of Special Loop Variables
n
| Variable | Description |
|---|---|
loop.index |
The current iteration of the loop, starting from 1. |
loop.index0 |
The current iteration of the loop, starting from 0. |
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 |
A boolean value that is `True` if it is the first iteration. |
loop.last |
A boolean value that is `True` if it is the last iteration. |
loop.length |
The total number of items in the sequence. |
loop.cycle(*args) |
A function to cycle between a list of sequences, useful for alternating styles (e.g., `loop.cycle(‘odd’, ‘even’)`). |
loop.depth |
Indicates the current nesting level in a recursive loop, starting at 1. |
loop.depth0 |
Indicates the current nesting level in a recursive loop, starting at 0. |
loop.previtem |
The item from the previous iteration. Undefined on the first iteration. |
loop.nextitem |
The item from the next iteration. Undefined on the last iteration. |
loop.changed(*val) |
A function that returns `True` if the value passed to it is different from the previous iteration. |
nn
nn
n
Demonstration with Code Samples
n
1. Using `loop.index` for Numbering
nThis is a straightforward way to create numbered lists or other sequential output without needing to manually track a counter. The example below shows how to use `loop.index` to create a numbered list from a sequence of data.nnJinja2 Templaten
{% set todo_list = ['Buy groceries', 'Walk the dog', 'Finish project'] %}n<ul>n{% for task in todo_list %}n <li>{{ loop.index }}. {{ task }}</li>n{% endfor %}n</ul>
nExplanation: The `loop.index` variable, which starts at 1, is printed before each task, resulting in a numbered list like “1. Buy groceries,” “2. Walk the dog,” and so on.n
n
2. Conditional Rendering with `loop.first` and `loop.last`
nYou can use these boolean variables to apply unique styles or logic to the first and last items in a list. This is a common practice for adding separators or special classes to elements.nnJinja2 Templaten
{% set items = ['Item A', 'Item B', 'Item C'] %}n<span>n{% for item in items %}n {{ item }}n {% if not loop.last %}n ,n {% endif %}n{% endfor %}n</span>
nExplanation: The `if not loop.last` condition ensures that a comma is added after each item, but not after the very last one. This prevents a trailing comma and makes the output cleaner.n
n
3. Zebra Striping with `loop.cycle`
nThe `loop.cycle` function is an elegant way to alternate between different values for each iteration, which is often used for “zebra-striping” table rows with different background colors for better readability.nnJinja2 Templaten
{% set users = ['Alice', 'Bob', 'Charlie', 'David'] %}n<table>n {% for user in users %}n <tr class="{{ loop.cycle('odd-row', 'even-row') }}">n <td>{{ user }}</td>n </tr>n {% endfor %}n</table>
nExplanation: The `loop.cycle(‘odd-row’, ‘even-row’)` function alternates between the two string values for each row. The first row gets `odd-row`, the second `even-row`, the third `odd-row` again, and so on. This allows you to apply different CSS styles to each type of row.nn
n
