Skip to main content

Jinja

Full documentation at: Jinja Template Designer Documentation

Filters

lower()

Convert a value to lowercase.

{{ value | lower }}

upper()

Convert a value to uppercase.

{{ value | upper }}

capitalize()

Capitalize a value. The first character will be uppercase, and all others will be lowercase.

{{ value | capitalize }}

map()

Apply a filter on a sequence of objects or look up an attribute. This is useful when dealing with lists of objects, but you are only interested in a certain value. The example usage is mapping on an attribute. Imagine you have a list of persons, but you are only interested in a list of names:

{% for person in persons|map(attribute='name') %}
{{ person }}
{% endfor %}


<!-- OUTPUT -->
Jane Doe
John Doe
"persons":[ 
{
"name": "Jane Doe",
"age": "30"
},
{
"name": "John Doe",
"age": "35"
}
]

reverse()

Reverse the object or return an iterator that iterates over it the other way round.

{% for person in persons|map(attribute='name')|reverse %}
{{ person }}
{% endfor %}


<!-- OUTPUT -->
John Doe
Jane Doe
"persons":[ 
{
"name": "Jane Doe",
"age": "30"
},
{
"name": "John Doe",
"age": "35"
}
]

unique()

Return a list of unique items from the given iterable.

{% for person in persons|unique(attribute='age') %}
{{ person }}
{% endfor %}


<!-- OUTPUT -->
{'name': 'Jane Doe', 'age': '30'}
{'name': 'John Doe', 'age': '35'}
"persons":[ 
{
"name": "Jane Doe",
"age": "30"
},
{
"name": "John Doe",
"age": "35"
},
{
"name": "John Doe",
"age": "35"
}
]

Date Formatting

Example date format to:

  • Day of the month as a zero-padded decimal number
  • Month as the locale's full name
  • Year with the century as a decimal number
{{ start_date.strftime('%d %B %Y') }}


<!-- OUTPUT -->
12 December 2023
{
"start_date": datetime.date(2023, 12, 25)
}

Resources:

Expressions

Inline if expression

{{ 'class1' if condition1 else 'class2' if condition2 else 'class3' }}

Translations

The most common way to create translations is to use the trans tag.
The given example creates a translation with the key Some text to be translated.

{% trans %}Some text to be translated{% endtrans %}

The Underscore

You can also use the underscore method for translations:

{{ _("Some text to be translated") }}

Plurals

It's also possible to create translations for strings that need pluralization, e.g., 1 product found / 2 products found.
This can currently only be done with the trans tag:
The given example will result in 1 product found if total_found is equal to 1. Everything else (including 0) will show the plural text.

{% trans count=total_found %}
{{ count }} product found {% pluralize %} {{ count }} products found
{% endtrans %}

String Formatting

There are multiple ways of string formatting within Jinja. This is used, for example, to create complex URLs or translations with different variables.

With a Function

{{ 'Hello {}!, this is the application talking to you!'.format('John') }}

With a Filter

{{ 'Hello %s!, this is the application talking to you!'|format('John') }}

With a %

{{ 'Hello %s!, this is the application talking to you!' % 'John' }}

The above code snippets will all result in:

'Hello John!, this is the application talking to you!'

Multiple Variables

It is also possible to format a string using multiple variables. Just be careful to always have the same number of variables passed as placeholders available.

That could look something like this:

{{ 'Hello {}!, this is the application talking to {}!'.format('John', 'Mary') }}

{{ 'Hello %s!, this is the application talking to %s!'|format('John', 'Mary') }}

{{ 'Hello %s!, this is the application talking to %s!' % ('John', 'Mary') }}

{{ _('Hello {name1}, this is the application talking to {name2}.').format(name1='John', name2='Mary')}}