Jinja - Addition and Increment (+)

by
Jeremy Canfield |
Updated: November 25 2024
| Jinja articles
Here is the most basic example of how to add two numbers in Jinja.
sum = {{ 1 + 1 }}
It is much more common to store the result in a variable.
{% set sum = 1 + 1 %}
sum = {{ sum }}
Which should return the following.
2
Increment
Here is an example of how to increment a value.
{% set count = 1 %}
original count = {{ count }}
{% set count = count + 1 %}
new count = {{ count }}
Which should produce the following.
original count = 0
new count = 1
An increment is often used as part of a for loop.
{% set count = namespace(value=0) %}
{% for item in list %}
count = {{ count.value }}
{{ item }}
{% set count.value = count.value + 1 %}
{% endfor %}
Did you find this article helpful?
If so, consider buying me a coffee over at