casting ints to str in Jinja2

You may use join:

{% set p = (year, month, day, post.slug)|join("/") %}

Jinja2 also defines the ~ operator, which automatically converts arguments to string first, as an alternative to the + operator.

Example:

{% set p = year ~ '/' ~ month ~ '/' ~ day ~ '/' ~ post.slug %}

See Other operators or, if you really want to use str, modify the Environment.globals dictionary.


To cast to a string in an expression, you use x|string() instead of str(x).

string() is an example of a filter, and there are several useful filters that are worth learning about.

Tags:

Python

Jinja2