Django Templates Leave me Wanting

Originally, I was going to post this to the Django mailing list, but I figured my thoughts wouldn’t be well received. <rhetorical-question>Does that make me a coward?</rhetorical-question> Anyway, here’s what I had to say:

I just want need to gripe about Django’s templating system. Seriously, why has everything resembling a decent programming language been intentionally omitted? I know one of the goals is to encourage separation of presentation and (non-presentation) logic, but I really feel like alot of the life has been choked out of it. One thing that would really help is if Python(-like) expressions were allowed.

One of the facts that points to my suggestion is the existence of tags such as ifequals. There’s absolutely nothing wrong with if a == b (except that you might confuse it with a = b). To the contrary, this is much better, because then there’d only be one way to do branching, instead of having all sorts of weird types of branching. I recently tried to do {% if d in foo.days %} checked{% endif %} in a checkbox input tag, and found out this isn’t allowed in Django templates. Trying to get around this actually made things more complicated.

Another thing I tried to do was loop over integers from 1 to 5 to avoid copy/pasting essentially the same HTML in a form:

{% for i in range(1, 6) %}
<input type=”checkbox” value=”{{ i }}” …>
{% endfor %}

This seems like a perfectly reasonable thing to do in a template. One (ugly) way to get around this is to pass in the range(1, 6) from the view. The problem with that approach is that in this case, looping 5 times is required by the presentation logic, not the “business” (non-presentation) logic. That means, we’re forced to mix presentation logic with other parts of the app, defeating one of Django’s central goals.

A similar situation happens when you need the length of something in a template. Suppose you were doing a list page, and you wanted to mention the number of items in the list. You’d have to pass the list as well as its length. This not only creates unnecessary work for the view (because lists keep track of their own length, unlike arrays in C), it mixes presentation logic into the view. Because of the presentation requirement of having to show a number (which could easily be derived from the list itself), the view has the additional responsibility of calculating the length for the template instead of allowing the template to do “You own {{ len(foobars) }} foobars. Congratulations!”.

I know there’s some concern about expressions getting too complicated, but that would be the fault of the programmer, not a limitation imposed by the language; whereas, in these examples (and I’m sure there are lots more), complexity is unavoidable due to the limitations of the language, not bad programming. I’m not totally sure allowing expressions would solve all the problems with Django templates, but it does seem like it would help alot.


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