Introduction tofor Loop in Jinja
n
for loop is a core control structure in Jinja that allows you to repeat a block of code for each item in a sequence. This is essential for rendering dynamic content, such as lists, tables, or navigation links, directly from data passed to your template.nn
nn
n
How It Works
nA for loop iterates over a sequence (like a list, tuple, or dictionary) and makes each item available as a variable within the loop’s block. The loop continues until all items in the sequence have been processed. You can also access special loop variables inside the loop to get information like the current index (`loop.index`), whether it’s the first or last item (`loop.first`, `loop.last`), and more.nnThe for loop syntax always starts with `{% for … %}` and ends with `{% endfor %}`. You can also use an optional `{% else %}` block to render content if the sequence is empty.n
Syntax
n
{% for item in sequence %}n <!-- Code to repeat for each item -->n{% endfor %}
n
{% for key, value in dictionary.items() %}n <!-- Code to repeat for each key-value pair -->n{% endfor %}
nn
nn
n
Demonstration with Code Samples
nHere are some practical examples of how to use the for loop in a Jinja2 application.n
1. Basic List Iteration
nThis is the most common use of the `for` loop, used to generate an HTML list from a simple list of data, as in the example below.nnJinja2 Templaten
{% set users = [{ 'username': 'user_a' }, { 'username': 'user_b' }, { 'username': 'user_c' }] %}n<ul>n{% for user in users %}n <li>{{ user.username|e }}</li>n{% endfor %}n</ul>
nExplanation: The template loops through each dictionary in the `users` list. For each item (which is a dictionary), it accesses the `username` key and creates a list item. The `|e` filter ensures that the output is safely escaped to prevent security vulnerabilities.nn
nn
n
2. Iterating Over a Dictionary
nWhen you loop over a dictionary, you can get both the key and the value for each item by using the `.items()` method, which is a common pattern in Python.nnJinja2 Templaten
{% set product_info = {'name': 'Jinja2 Book', 'price': 29.99, 'in_stock': true} %}n<ul>n{% for key, value in product_info.items() %}n <li><strong>{{ key|capitalize }}</strong>: {{ value }}</li>n{% endfor %}n</ul>
nExplanation: This loop iterates through the `product_info` dictionary. In each iteration, it assigns the key to the `key` variable and the value to the `value` variable, dynamically creating a list of product details. The `|capitalize` filter is used to format the key for display.nn
nn
n
3. Using loop Variables for Conditional Rendering
nThe loop variable gives you access to the current state of the iteration, which is useful for applying special styling or logic to specific items in the loop.nnJinja2 Templaten
{% set menu_items = ['Home', 'About', 'Contact'] %}n<ul>n{% for item in menu_items %}n <li{% if loop.last %} class="last-item"{% endif %}>{{ loop.index }}. {{ item }}</li>n{% endfor %}n</ul>
n nnExplanation: In this example, `loop.index` provides a 1-based index of the current item, and `loop.last` is a boolean that is `true` only for the final item. This allows the template to add a special class to the last list item, which could be used for styling in CSS.nn
n
