Friday, January 8, 2010, in which we circumvent Django's template system

The whole separate-code-from-presentation imperative that inspired Django's template system ... let's say I have my doubts. From my experience, Django's particular approach to templates diverts much too much of my attention to circumventing its limitations.


In response to Jake's observation that "variables should always be set in the view, not the template," in general, I agree.

But the specific counter-example I'll supply is the very common title example. Let's say you'd like to create a base template like so:

<head><title>{% block title %}</title></head>
<h1>{% block title %}</h1>

And then you inherit from that template:

{% block title %}User Feedback{% endblock %}

This is a very simple, commonplace scenario, and it's simply unsupported. Django will error out on the second call to {% block title %}, and I think that's dumb.

1 comment:

Jake said...

Django's template system is designed to promote what the Django guys consider to be proper technique. Variables should always be set in the view, not the template. But the Django guys didn't want to be too draconian so they let you create whatever custom template tags you want. Personally I think the system strikes a pretty good balance, but I haven't been coding as long as you ;)