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.)

Generating List Of Mustache Tags In A Template

Google falls over again looking for:

“generating list of mustache tags in a template”
“regular expression list of mustache tags in a template”
“grep list of mustache tags in a template”
“get list of mustache tags in a template”
“grep expression for mustache tags”

What am I trying to do? I have a Mustache template, and I want to pull out the list of tags I am using in it, because I need to filter a bigger chunk of JSON data to only contain those tags. This JSON data will eventually be used as the context that is then used to .render() the output.

Google needs to reinstate exact-quote indexing, the fuzzy searches of the past few years are making it really hard to find relevant links. (Yes, I probably should have the regex rules memorized, but this can’t be the first time someone’s been looking for this.)

For the record, the command I used to generate this list:

Update 2 March 2013:

The above command was perhaps a bit too simple, it gives the tags in the form of a list of “{{tag}}”.

To get rid of the mustache brackets, you have to run the point through sed.

In Python, the following regex will also give you all the tag names, sans brackets:

Implicit Things

As a learning exercise, I converted a piece of Javascript code i’d written into CoffeeScript.

So what’s wrong with this?

Oh that’s right, initialize doesn’t get called.

It must be “initialize()” when you want to call a function w/no params. But when you do have params, you can leave the parens off. What inconsistent bullshit is this? I guess it’s nice that “initialize” by itself is a statement w/o a side effect. But then when you run it through the compiler (when the hanging return isn’t there), it spits out “return initialize;”, which isn’t what I meant at all and could be a side effect for anyone trying to maintain this down the road. Better to make it explicit that this thing won’t return anything a caller can (ab)use, until and unless I choose to make that the case.

So I’ve been adding hanging returns so the compiled code returns nothing as often as it should.

Sigh, I don’t know that I dig the whole implicit “return whatever was on the last line” thing that so many of these terse scripting languages have. And it strikes me as a weird idea to try and save on parens.

So the CoffeeScript adventure begins, but I’ll be damned if I’m not already building the defensive programming idioms in.

emacs Settings

emacs is incredibly powerful, but configuration can be a royal pain sometime.

Here’s me trying to set the indentation levels when editing HTML. (Because it’s not obvious at all.)

Go to Nxml Child Indent, set that to what you want. In my case, I use 4 spaces per TAB.

These are the settings in my .emacs (you can see that I’m really trying to just set 4 spaces per TAB for all the modes):

Emacs can be annoying in the sheer number of uncorrelated variables it uses for the various modes.

Building An Assets Subdirectory When Using Brunch

Brunch has an opinion about the app/assets/ directory that it generates as part of an app skeleton. It simply copies these files over to the deployment directory, w/o running them through any minification.

But let’s say I want to build a standalone page under the assets/ directory, maybe for a signup form for my website (which I don’t really feel like making into a Backbone View). This form could share the vendor/ minified files, but would not have a need for the app.js or app.css files.

By default, Brunch wouldn’t process those files, as they would be somewhere under, say, app/assets/signup.

Here’s a custom config_signup.coffee file that builds files in the assets/signup/ directory and copies them where I expect. It would be pretty sweet if this could be somehow chained onto the main CoffeeScript file.

Now when I run brunch build -c config_signup.coffee –minify, I can get the same processing on this particular subset of the site.