Mixing Django with Jinja2 without losing template debugging

At Fashiolista we’ve build nearly the entire site with Jinja instead of the Django template engine.

There are a lot of reasons for choosing Jinja2 over Django for us. Better performance (atleast… it was a lot better with previous Django versions), way more options (named arguments, multiple arguments for filters, etc), macros and simply easier to extend. Writing custom tags is simply not needed anymore since you can just make any function callable from the templates.

But… during the conversion there are always moments when you need  a Django function in a Jinja template or vice versa. So… I created a few template tags to allow for Jinja code in Django templates (I’ve also created code to run Django code from Jinja, but I haven’t seen the need for it so I omitted it here).

A Jinja Include tag to include a template and let it be parsed by Jinja from a Django template:

Usage:

A couple of noop nodes to make sure that when you convert your Jinja templates to be executed from Django, they won’t break because of the missing Django tag.

And the Jinja tag to allow Jinja blocks in Django templates.

Do note that I have modified the “coffin.template.Template” to enable debugging completely. Just replace the “Template” class in “coffin/template/__init__.py” to make it work.

And you’re done, now you can just mix your Django and Jinja templates like this:

Bookmark and Share

Tags:

About Rick van Hattem

Rick van Hattem is a Dutch Internet entrepreneur and co-founder of Fashiolista.com

5 Responses to “Mixing Django with Jinja2 without losing template debugging”

  1. raacer | 2013-10-23 at 02:15:51 #

    Thank you for usefull snippet 🙂

  2. Anup | 2014-01-26 at 00:15:35 #

    Thanks for this post. You had mention “(I’ve also created code to run Django code from Jinja, but I haven’t seen the need for it so I omitted it here).” Is there a way you could publish that code I am in a situation where 3rd party django project that i am using uses jinja2 and I have written django custom template which i want to get it working with jinja2. Thanks,

Trackbacks/Pingbacks

  1. Mixing Django with Jinja2 without losing template debugging | Django Daily - 2013-12-02

    […] Since I have finally set up my own blog this post has been moved to: http://w.wol.ph/2013/07/28/mixing-django-with-jinja2-without-losing-template-debugging/ […]

  2. django, jinja, jinja2, coffin, template, templatetag, tags - 2014-01-29

    […] Wolph on Mixing Django with Jinja2 without losing template debugging […]

Leave a Reply to raacer Click here to cancel reply.