Bootstrap FreeKB - Jinja - Addition and Increment (+)
Jinja - Addition and Increment (+)

Updated:   |  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 while loop.

{% set count = namespace(value=0) %}
{% for item in list %}
  {{ item }}
  {% set count.value = count.value + 1 %}
{% endfor %}

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 6adfe4 in the box below so that we can be sure you are a human.