Jinja2

Jinja2 is very similar to DTL in syntax. But it has a slightly different philosophy in certain places. For instance, in DTL the method call is implied as in the following example:

{% for post in user.public_posts %} 
    ... 
{% endfor %} 

But in Jinja2, we invoke the public_posts method similar to a Python function call:

{% for post in user.public_posts() %} 
    ... 
{% endfor %} 

This means that in Jinja2 you can call functions with arguments, unlike DTL. Refer to the Jinja2 documentation for more such subtle differences.

Jinja2 is usually chosen for the following reasons:

  • Familiarity: If your template designers are already comfortable using Jinja2
  • Whitespace control: Jinja2 has finer control over whitespace after the tags get rendered
  • Customizability: Most aspects of Jinja2, from string defining markup to extensions, can be easily configured
  • Performance: Some benchmarks show Jinja2 is faster than Django
  • Autoescape: By default, Jinja2 disables XML/HTML autoescaping for performance

In most cases, none of these advantages are overwhelming enough to use Jinja2. This also goes for using other templating engines such as Mako or Genshi.

The familiarity of using DTL reduces the learning curve to anyone new to your project. It is also well integrated and tested. Finally, you might have to replicate Django-specific template tags such as static or url.

Unless you have a very good reason not to, I would advise sticking to Django's own template language. The rest of this chapter would be using DTL.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.183.172