Andy McKay

Apr 09, 2008

Django templating

This comment from the Django documentation was pointed out to me tonight and it winds me up a bit:

Why use a text-based template instead of an XML-based one (like Zopes TAL)? We wanted Djangos template language to be usable for more than just XML/HTML templates. At World Online, we use it for e-mails, JavaScript and CSV. You can use the template language for any text-based format.

Oh, and one more thing: Making humans edit XML is sadistic!

Django templates

There's a few things in this comment that annoy me.

Most websites are written as HTML. Almost every site these days is XHTML. And XHTML is "A Reformulation of HTML 4 in XML 1.0". At this point yes I'm aware, XHTML isn't quite XML as this article points out, but it's as near as damn it. Everyone writes XHMTL these days. In fact look at the heading HTML of the Django admin interface:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
<html xmlns=""
 lang="en-us" xml:lang="en-us" >

So how can you write an interface in XHTML and claim that forcing people to write in XML is sadistic? The difference is so small to be neglible..

Next, you use it for "e-mails, JavaScript and CSV". Excellent, I put together sites using XHTML, Ajax (XML), RSS (XML), e-mail, Javascript, CSV's and PDF's. I've almost never, in the many sites I've done, started formatting Javascript, CSV's or PDF's using a templating language - the only exception I can think of is DTML which CMF and Plone (and I didn't write it).

In fact in Plone e-mails are formed using Page Templates, here's a snippet:

From: "<span tal:replace="portal/emailfromname" />" 
<span tal:replace="structure lt"/><span tal:replace="portal/emailfromaddress" />
<span tal:replace="structure gt"/>
mailpasswordtemplate line breaks added for readability

I would accept that is a little complicated and personally I've always found using python string formatting (the % operator) more than sufficient. So we don't need Django templating for that either. But let's face it, in most sites I have done, 95% of the templating demands are for HTML. I bet yours are too. So focus on what you need to do the most and build good tools that allow you to do it fast and simply with great flexibility.

Throwing out a great tool for an edge case, which it allegedly can't meet, is not a good answer. There's better reasons for not using Page Templates and I bet in any feature comparison, Page Templates would beat Django templates. Just say "I don't like Page Templates, it's ok".