This article has some article blocks

The ProPublica News Apps Team explains how they built a 'Tangled Web' of election contribution data

If you have previously worked with Django templates, you should find Jinja2 very familiar. In fact, most of the syntax elements look and work the same.

However, Jinja2 provides some more syntax elements covered in the documentation and some work a bit different.

This section covers the template changes. As the API is fundamentally different we won’t cover it here.

Method calls

In Django method calls work implicitly. With Jinja2 you have to specify that you want to call an object. Thus this Django code:

{% for page in user.get_created_pages %}
...
{% endfor %}

will look like this in Jinja:

{% for page in user.get_created_pages() %}
...
{% endfor %}

This allows you to pass variables to the function which is also used for macros which is not possible in Django.

Conditions

In Django you can use the following constructs to check for equality:

{% ifequal foo "bar" %}
...
{% else %}
...
{% endifequal %}

In Jinja2 you can use the normal if statement in combination with operators:

{% if foo == 'bar' %}
...
{% else %}
...
{% endif %}

You can also have multiple elif branches in your template:

{% if something %}
...
{% elif otherthing %}
...
{% elif foothing %}
...
{% else %}
...
{% endif %}

Filter arguments

Jinja2 provides more than one argument for filters. Also the syntax for argument passing is different. A template that looks like this in Django:

{{ items|join:", " }}

looks like this in Jinja2:

{{ items|join(', ') }}

In fact it’s a bit more verbose but it allows different types of arguments - including variables - and more than one of them.