Home Login Create account Contact Help
Close

Login to your account

Other Options

Comments feed

In response to XML or Text for Python Templates?

There are a number of (very good) templating systems and languages available for Python. They fall in to one of two camps; either they are XML based, like Genshi, or they are text based, like Mako. Most programmers favour one or the other, but there is far from a consensus over which is better.

I'd like to use this debate to gather reasons for using one over the other in the context of web development. I suspect there will be no clear winner, but it should serve as a useful resource for those faced with the decision!

...

NB. You can post code with the [code] bbcode tag. Many languages are supported. e.g.

[code python]

print "Hello, World!"

[/code] 



Anthony Briggs argues for "XML"
Wed, 17 Oct 2007 by anthony
Rank 6. Average 0.0 ( 3 votes )
XHTML templates are clearer than text-based ones.

XHTML templates are clearer than text-based ones. Compare the following sample code:

<ul>
% for item in items:
<li>${item.name}</li>
% endfor
</ul>
<ul>
% for item in items:
<li>${item.name}</li>
% endfor
</ul>

<ul>
<li tal:repeat="items" tal:content="item/name">Item Name</li>
</ul>
<ul>
<li tal:repeat="items" tal:content="item/name">Item Name</li>
</ul>

Extrapolate that to a real project, and I wouldn't like to be the one looking at pages and pages of <% ${foo.bar} %>

[ Modified the code tags to look prettier. - admin ]

Will McGugan comments...
Thu, 18 Oct 2007 by admin
You are asserting that one is clearer than the other without offering any reasons why. People that are unfamiliar with either system wouldn't necessarily see much of a difference in clarity.
Anthony Briggs comments...
Thu, 18 Oct 2007 by anthony
According to Will McGugan:

You are asserting that one is clearer than the other without offering any reasons why. People that are unfamiliar with either system wouldn't necessarily see much of a difference in clarity.


Hmm, seems pretty obvious to me, but the main reason is the amount of extra punctuation that text-based templates seem to need, combined with dropping in and out of python/HTML.

In the above example (and it's deliberately a simple example), you've got a couple of % signs, and a dollar sign with a couple of squiggly brackets, in addition to all of the HTML markup.

Perhaps it seems like a small thing, but to me it's more like the difference between Python's syntax and Perl's. In other words:

xml-based looks like python
text-based looks like perl


Return to debate