Custom Pystache Template And Loader Classes For Django

Update: I’ve posted the code at github here.

Wanting to keep my server-side and client-side templates identical, I noticed there wasn’t a good solution for using Mustache/Pystache/Handlebars templates alongside the Django templating language.

The existing one that I saw used Pystache internals, and I’m not even sure they’re still valid in the latest Pystache versions.

The other existing one that I saw let you do the equivalent of #include “something.mustache” within the Django templates, which wasn’t what I was looking for either.

So I went ahead and wrote a set of PystacheTemplate and Pystache*Loader classes which do it all within the specifications of the Django infrastructure, and, I set them up so that you can specify the exact file extensions you want them to operate on, i.e. just .mustache, .handlebars, and .hbs files by default. This doesn’t mean you can mix Mustache directly into Django templates, which doesn’t make sense (and is mildly redundant), but it does mean you can render complete Mustache template files directly, which is awesome if you’re wanting to share those templates or partials between client and server.

Copy this file somewhere into your project or app folders:

Then you can just update the settings.py file to use the custom Loaders, putting them ahead of Django’s default Loaders in the TEMPLATE_LOADERS setting:

Then if you have, say, a file called index.handlebars somewhere in one of your app directory template/ folders or somewhere else in the TEMPLATE_DIRS list, you can just do this in one of your views:

And all will be good.

Please let me know in the comments if you have any problems with this. (Speed might one of them, I haven’t extensively tested this yet. But I plan to cache these using the Django template cache loader.)

6 thoughts on “Custom Pystache Template And Loader Classes For Django”

  1. Will this work if you are using template inheritance with an include or block? Or do I need to create a custom template tag? As in:

    {% include “_sidebar.mustache” %}

    1. Good question. I never tried that from a Django-style template. It’s worth a shot, if Django does the sensible thing and just loads the included template and passes it the same RequestContext as the initial template, then it could work.

      You could also try using partials in the Mustache templates, if that’s an option.

      I have to admit that I eventually ported all of my Mustache templates back to Django-style templates. Turns out, view logic in the templates was more sensible than forcing all the logic into views.py, and cut down on a lot of boilerplate code.

      The dream of using a single type of templates for both client and server side never seemed to come to fruition for me. And Django threw in a healthy number of WTFs along the way.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.