Personal tools
You are here: Home Weblog Archive 2009 November 18 If Django templates are an improvement over XML templates, then, by all means, please give me XML
Document Actions
  • Send this page to somebody
  • Print this page
  • Add Bookmarklet

If Django templates are an improvement over XML templates, then, by all means, please give me XML

by tibi last modified 2009-11-18 15:28
Filed Under:

I fail to see how

{% block %} 
...
{% endblock %}

is in any way better or "less scary" then, let's say

<dj:block>
...
</dj:block>

Yet another rant, this time triggered by the error I got when writing this piece of code:

{% blocktrans with offer.offerer.username as offerer_username 
               and offer.offered_time|date as offerer_date %}
...
{% endblocktrans %}
I just wanted to split the tag on multiple lines, but it seems that's not possible. If Django templates would have been XML, then it wouldn't have been any problem formatting that piece just how I want it. Right now, the joined line takes two times the amount of my screen width.

One more thing to grudge about is that vim, even with djangohtml syntax type installed, is not very knowledgeble about how to format the template file (it treats the tags as regular piece of text). Probably this could be fixed, so I shouldn't complain about this too much.

I think Django templates are compiled to python code, so it's natural that they're treated in an imperative, dumb way, but that's not the only way of doing things. For example, Chameleon is another templating library that compiles its templates as python code, has no problem working with an XML based templating language frontends.

If you want to put more XML in XML, then, by all means, do it!

Posted by Anonymous User at 2010-02-03 13:41
1. When using anything other than XML templates it's much easier to separate the tags/blocks that are specific for the template language from all the rest of the code. Putting more XML tags in HTML/XHTML code just turns down the signal to noise ratio.
2. XML templates are only good for generating valid XML. When using Genshi for example, you have to fall back to "text templates" which is a completely different syntax and far from XML, when you want to generate a JS file for example.

I don't get your argument. After writing tons of Django and Jinja2 template code over the years I've never faced a similar formatting problem. And this is from someone who's a code formatting nazi.

My reply to your points

Posted by tibi at 2010-02-05 13:11
1. Maybe the years of using TAL conditioned me, but I tend to place higher value to the HTML structures then to the dynamic bits, as I use the html structures as cues to navigate the template. Using TAL you're not gonna place more XML tags then you would normally have (well, you're gonna have more XML attributes). On the contrary, the template can become leaner. I've illustrated an example here: http://dpaste.com/154885/

2. As a web developer, my templates will almost always end up as xhtml files. Dynamic resource files (CSS, JS) are very rare, as they can be tricky to get it right with regards to caching and almost always the thing that needs to change inside them can go in a regular template. The only thing that justifies having a plain text template would be email notification messages, but with the plethora of choices for templating libraries (or even Python builtins), it's trivial to find and chose a simple one for just that use case.

In the end, it all comes down to what you're used to. For sure both approaches are valid. As for never having encountered the problem I've exposed in the article, maybe you haven't dealt with i18n yet. Look at the example and tell me what I could differently.

Maybe you are trying to put too much stress on the templates

Posted by Anonymous User at 2010-02-25 03:41
I'll just comment on your specific problem. Yes, I've used i18n in Django extensively and I am the main translator for one of the languages Django supports. I've never felt the need to use "with" withing a blocktrans tag, that's what I was saying. Maybe you are putting too much stress on the template language. I guess if you've worked with Genshi or Mako for some time the DTL may seem too limited. But the thing is it's designed so that it dicourages people to put much logic in the templates. When working with Django, I put most of my presentation logic in Python code withing custom templates and filters and put my html withing templates.

Explanation...

Posted by tibi at 2010-02-25 15:37
Most of the translation work I've done was fixing some of Pinax's templates so that they would be properly translatable. The reason that I have used "with" in blocktrans was, because as you can see, you can't have anything except variable names in the blocktrans placeholders. Now, of course I might be able to put define those variables in the template context, but that would negate using template filters and so on. I still stand my ground on this issue.
Weblog
Atom
RDF
RSS 2.0
Powered by Quills
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: