Using Django templating engine I can include another partial template while setting a custom context using named arguments, like this:
{% include "list.html" with articles=articles_list1 only %}
{% include "list.html" with articles=articles_list2 only %}
As you may be supposing, articles_list1
and articles_list2
are two different lists, but I can reuse the very same list.html
template which will be using the articles
variable.
I'm trying to achieve the same thing using Jinja2, but I can't see what's the recommended way, as the with
keyword is not supported.
Updated 2021+
Included templates have access to the variables of the active context by default
. For more details about context behavior of imports and includes, see Import Context Behavior.
From Jinja 2.2 onwards, you can mark an include with ignore missing
; in which case Jinja will ignore the statement if the template to be included does not exist. When combined with with
or without context
, it must be placed before the context visibility statement. Here are some valid examples:
{% include "sidebar.html" ignore missing %}
{% include "sidebar.html" ignore missing with context %}
{% include "sidebar.html" ignore missing without context %}