In addition to Django templates, the Django framework also supports Jinja templates. Jinja is a standalone template engine project that's very similar to Django's built-in template system.
However, the adoption and growth behind Jinja templates in Django projects is in part due to the design limitations of Django templates which have changed little to nothing since Django's creation.
Jinja advantages and disadvantages
In order for you to gain a high-level perspective of Jinja templates and learn if they're a good fit for your Django projects, I'll first enumerate some of the main advantages and disadvantages of Jinja templates. Let's start with the advantages:
- Speed & performance.- Jinja compiles template source code to Python bytecode when it's first loaded, so the template is only parsed once, resulting in better runtime performance. In addition, Jinja also supports the option of ahead-of-time compilation, which can also result in better performance.Although speed & performance are some of the most debatable advantages to Jinja templates, given the many factors affecting speed & performance benchmarks (e.g. database queries/load, server configuration). Generally speaking and all things being equal, a Jinja template that does exactly the same thing as a Django template, the Jinja version will be faster than the Django version.
- Flexibility.- Jinja templates are very flexible in terms of what they can contain, supporting concepts like macros and more Python-like constructs. While some of these practices are discouraged in web templates, you'll come to appreciate some of these features which are not available or severely constrained in Django templates.
- Similar to Django templates.- Jinja is actually inspired by Django templates, so there's a lot of common ground between the two systems. Powerful features like template inheritance and blocks work in the same way, so there's a smaller learning curve to use Jinja in Django projects than you might realize. In addition, security features (e.g. auto-escaping) are also tightly integrated into Jinja, just like they are in Django templates.
- Asynchronous execution.- Templates can sometimes load a lot of data or use functions that take a long time to run, causing delays in templates that must 'wait' for backing tasks to finish (i.e. they're synchronous). Jinja templates support asynchronous execution, which allows backing tasks to run their course -- without holding-back templates -- and later reconvene with templates when finished. Note this feature requires the use of asynchronous generators which is only available in Python 3.6 or newer releases.
And now some Jinja template disadvantages:
- Little to no third party package support.- Because official Django support for Jinja templates is relatively recent -- since Django 1.8, the prior long-term-support(LTS) version to Django 1.11 on which this book is based on -- almost all third party packages (e.g. Django admin) are still designed with Django templates. This can make it difficult to have a pure Jinja template Django project and require that Jinja templates coexist alongside Django templates, which can in turn lead to difficulties and confusion when template customization is required.
- New concepts.- If you're accustomed to Django templates, some Jinja features require additional practice to understand and use correctly (e.g. Jinja macros, Jinja filters). Although this shouldn't be an issue if you're new to Django in general, as every concept is new and will require some practice.