djangodjango-templatesjinja2

Is Jinja a superset of the Django template language?


According to the project documentation, Jinja2 templates are 'modelled [sic] after Django’s templates.' I read elsewhere that Django templates can be used interchangeably with Jinja2.

So, I suppose the title says it all: are Jinja2 templates a (strict) superset of Django's native templates?

Otherwise, how do they differ? And if the differences are minor, why isn't Jinja2 a superset of Django's native templates?


Solution

  • No, Jinja is not a strict superset of the Django Template Language. A good summary of the differences is given in the Jinja documentation.

    There's no particular reason why Jinja would be a superset of the DTL. It's meant as a stand-alone package that can be used by any Python web framework. I would say that most of the differences are due to Jinja's attempt to be less restrictive and more Pythonic than the DTL. For example, while Jinja filter syntax uses the usual function call parentheses, the DTL uses a combination of | and :. (The DTL has slowly changed over time and is a bit more Pythonic than it used to be.)

    Jinja is officially supported by Django, and even before that there were third-party packages that allowed you to use it. The only real compatibility issue is that you can't use Django's custom template tags in a Jinja template. That could come up if you were using Jinja for your template language but wanted to use a third-party package that that provided functionality with a custom template tag.