Jinja - Getting Started with Variables

by
Jeremy Canfield |
Updated: December 09 2023
| Jinja articles
set is used to create a variable in Jinja.
{% set foo = "bar" %}
foo = {{ foo }}
In this example, the output should be
foo = bar
The + (plus) character is used similar to how the plus character is used in Python, to toggle between literal strings and Jinja variables.
{% set name = "John Doe" %}
{% set greeting = " Hello " + name %}
Let's say you set multiple variables.
{% set month = '01' %}
{% set day = '10' %}
{% set year = '2023' %}
Here is how you could create a variable using multiple other variables.
{% set date = month+day+year %}
Of, if you have an int (integer) object, you may want to use ~ (tilde) instead of + (plus) as ~ (tilde) will convert the int object into a string.
{% set mynumber = " Your number is " ~ number %}
Did you find this article helpful?
If so, consider buying me a coffee over at