Django Templates Could Suck Less

A picture is worth 1 kilowords. Here’s one that I came up with that might do a better job explaining one of my previous posts about what’s wrong with Django templates (actually, that post was 498 words). Basically, my argument was that Django’s decision to disallow the use of regular Python expressions was a huge mistake. Here is a screenshot from Django’s tag + filter documentation:

The highlighted sections show all the special cases that had to be invented because Django templates have an irrational fear of Python expressions and/or seem to believe they are the root of all evil. When you have this many special cases that can be dispensed with but for some strange religious belief, you have to question yourself. Does this really make sense? I can understand how it might have been difficult to realize at first why this doesn’t work, but it should be pretty dang obvious by now. Not only is there no reason for things to be this complicated, it probably defeats the original purpose, which is to allow designers to read and maybe even edit templates.

Frankly, I’ve always been skeptical of the claims that a template language is suitable for designers to work with, even when they have little or no programming experience. How is that even beneficial? I used to work with a designer who did his work in Photoshop. When he had something for me to work on, he’d just send me pictures, which I’d have to translate into templates + CSS + JavaScript. This a perfectly reasonable workflow. In fact, he’d probably do a much worse job if he had to write his design in text editor instead (I don’t think you can write Django templates in Dreamweaver), because a text editor does not show you what the dang thing is actually going to look like when you load it in a browser. People (even artists) simply don’t always know what looks good until they actually see it. They need a tool that allows them to instantly see what they’re doing and not have to switch back and forth between windows + hit the reload button (or press f5).

If your designer knows HTML + CSS and likes to work in Dreamweaver, that just makes your life a little easier. Now, all you have to do is insert some Django template language, and viola, you’ve got a dynamic page. Of course, in practice, the machine generated html that Dreamweaver spits out might be so ugly that you end up having to rewrite it, but that isn’t so bad, because, you’re now back to my previous case, which I argued is not really bad at all.

Even if I haven’t convinced you that letting your designer write your templates isn’t that great, templates with Python expressions don’t have to be hard to read for graphic designers (the other goal behind this misguided design decision). In fact, it can be pretty hard to read Django templates esp when you have to work around their “no expressions” limitation. Here’s an example straight from the documentation:

{% if messages|length >= 100 %}
   You have lots of messages today!
{% endif %}

If you don’t know that | means “feed the thing on the left into the thing on the right” or that >= means “greater than or equal to”, this is going to be pretty confusing. The best thing a designer can do in this situation is to completely ignore the if condition, because it’s going to be much easier to guess from the body what this section is about.

The following is just about as hard for a designer to read, but easier for a programmer (regardless of language):

{% if len(messages) >= 100 %}
   You have lots of messages today!
{% endif %}

The question is, who is going to have to work with this file more? A few paragraphs back, I argued that in the real world, graphic designers do not deal with template files, because the tools for working with Django templates (i.e. text editors) are completely inferior to Photoshop for the kind of work that they do. <bias>In that case, we should make things easier for programmers.</bias>

Having a ton of special cases actually gives us the worst of both worlds: more complexity + less flexibility. If I’m a Python programmer, and you tell me “just write if statements like you would in Python, except drop the semicolon, and use endif to close”, I’m going to have much less trouble trouble learning your template language, because it builds on what I already know. With Django templates, you might as well throw everything out the window, because there is a whole bevy of if forms that only do a (proper) subset of what Python if’s do. The problem here is not Python expressions, it’s complexity.

PS: I’m a now past 1 kilowords now, if you add the word count from my other post.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s