Home Login Create account Contact Help
Close

Login to your account

Other Options

Tags: Programming Python Templates Web Xml

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] 




Log in to post an argument Subscribe to feed
Rank order
XML
XML is an advantage for templating web content
Martijn Faassen argues...
Mon, 15 Oct 2007 by faassen
Rank 1. Average +1.6 ( 5 votes )
When programming (X)HTML templates, it is important for security reasons to escape any user-generated input, as otherwise users may be able to sneak arbitrary HTML into a page that other uses see. XML-based templating languages such as ZPT or Genshi support automatic escaping by default. Text-based templating languages such as Django's and Mako apparently do not. The knowledge of the underlying XML structure may make automatic escaping more easier to implement. While the lack of this security feature may not be an inherent shortcoming of text-based templating languages, it does at least seem to be a cultural difference.
metapundit argues...
Mon, 15 Oct 2007 by metapundit
Rank 2. Average +1.0 ( 5 votes )
The ability to preview templates directly in a browser shouldn't be minimised!
The biggest advantage of XML based templates is previewability (to coin a new word). In a typical XML based template system I can create an entire HTML page (head section and all) with relative links to related javscript, CSS, and image resources. I can insert dummy content into nodes in the document like
<h1 template:replace="title">This will be the Headline</h1>

<h1 template:replace="title">This will be the Headline</h1>

and preview the whole thing in a browser without even involving a webserver. Specify a rewrite path or two and the same template will be transformed to point paths to the right server locations - you can seamlessly switch between viewing static files locally and deploying templates remotely. The ability to preview templates directly in a browser shouldn't be minimised!
Simon Willison argues...
Mon, 15 Oct 2007 by simonw
Rank 3. Average +0.8 ( 6 votes )
If your output is XML, it makes sense for your input to be XML as well. XML template engines can guarantee that their output will be well-formed XML; text engines can only produce valid XML if the templates authors are incredibly careful, and can't gaurantee well-formed output. If your application is ouputting mainly XHTML, RSS and the like an XML template engine is a really good idea.
Isaac Csandl argues...
Wed, 17 Oct 2007 by nerkles
Rank 4. Average +1.3 ( 3 votes )
appealing for the sake of consistency
For web apps, XML in and XHTML out is appealing for the sake of consistency, and the templates are (at least potentially) more designer-friendly to work with, and can be opened directly in a browser (assuming you don't use any xincludes or matching).
Anthony Briggs argues...
Wed, 17 Oct 2007 by anthony
Rank 5. Average +2.0 ( 2 votes )
(X)HTML based templates allow you to split work between web designers and programmers.
(X)HTML based templates allow you to split work between web designers and programmers. The designers can do their design/mock up thing without having to know the code, then the developers can add the code parts later on.

With text-based templates, you have to break up the HTML in order to turn it into a template, so your maintenance phase (ie. a site redesign, or adding extra page elements) becomes much harder than it should be.

From a business-head perspective, it's also much easier to find a good programmer and a good designer, than it is to find those freaks of nature who can do both.
Anthony Briggs argues...
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 ]
Martijn Faassen argues...
Mon, 15 Oct 2007 by faassen
Rank 7. Average +1.0 ( 2 votes )
Editor integration: an XML-based template language may be better supported in an XML editor or XHTML editor. If the editor is unfriendly, it may still end up destroying the content still, of course, but the chances of this happening may be decreased.
Martijn Faassen argues...
Mon, 15 Oct 2007 by faassen
Rank 7. Average +1.0 ( 2 votes )
XML-based transformation tools such as XSLT, but also hand-written Python DOM manipulators or SAX parsers, work with XML-based templates out of the box. This may help if a large number of templates exist that need to be transformed according to common guidelines. Of course text-based transformation tools can be used for text-based templates as well, but these will have more limited knowledge of the structure of the underlying pages and may be more error-prone. In addition such text-based techniques of course still work with XML-based templates.


Martijn Faassen argues...
Mon, 15 Oct 2007 by faassen
Rank 8. Average +0.5 ( 2 votes )
XML and XHTML validation tools work out of the box with XML-based templates. Text-based templates do not. If faced with the task of validating a large number of templates according to some schema, it will be easier with XML-based templates.

John Millikin argues...
Sat, 29 Mar 2008 by jmillikin
Rank 9. Average 0.0 ( 0 votes )
XML-based templates allow serving valid HTML and XHTML pages from the same template source. Consider the following Genshi template:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns:py="http://genshi.edgewall.org/"
      xmlns="http://www.w3.org/1999/xhtml"
      xml:lang="en">
	<head>
		<title>Page title here</title>
		<link href="stylesheet.css" type="text/css" rel="stylesheet" />
	</head>
	<body>
		<div>Body content here</div>
	</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns:py="http://genshi.edgewall.org/"
xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en">
<head>
<title>Page title here</title>
<link href="stylesheet.css" type="text/css" rel="stylesheet" />
</head>

<body>
<div>Body content here</div>
</body>
</html>

This template is valid XML, and can be reliably transformed to either XHTML or HTML based on HTTP_REQUEST. By using XML templates, the template author is insulated from the exact output format a browser can best use, and can write/serve XHTML pages while still catering to the subset of users on legacy browsers.

Here is a Python session showing serialization to three distinct output formats, with no changes whatsoever to the template. They are all valid - paste them into the W3C Markup Validator [validator.w3.org] to confirm this.
>>> # Save above template as template.xml
>>> import genshi, genshi.output
>>> template = genshi.XML (file ('template.xml', 'rb').read ())
>>>
>>> # XHTML 1.0
>>> print template.render ('xhtml', doctype = genshi.output.DocType.XHTML)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:py="http://genshi.edgewall.org/" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
        <head>
                <title>Page title here</title>
                <link href="stylesheet.css" type="text/css" rel="stylesheet" />
        </head>
        <body>
                <div>Body content here</div>
        </body>
</html>
>>>
>>> # HTML 4
>>> print template.render ('html', doctype = genshi.output.DocType.HTML)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
        <head>
                <title>Page title here</title>
                <link href="stylesheet.css" type="text/css" rel="stylesheet">
        </head>
        <body>
                <div>Body content here</div>
        </body>
</html>
>>>
>>> # HTML 5
>>> print template.render ('html', doctype = genshi.output.DocType.HTML5)
<!DOCTYPE html>
<html>
        <head>
                <title>Page title here</title>
                <link href="stylesheet.css" type="text/css" rel="stylesheet">
        </head>
        <body>
                <div>Body content here</div>
        </body>
</html>
>>> # Save above template as template.xml
>>> import genshi, genshi.output
>>> template = genshi.XML (file ('template.xml', 'rb').read ())
>>>
>>> # XHTML 1.0
>>> print template.render ('xhtml', doctype = genshi.output.DocType.XHTML)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:py="http://genshi.edgewall.org/" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Page title here</title>
<link href="stylesheet.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div>Body content here</div>
</body>
</html>
>>>
>>> # HTML 4
>>> print template.render ('html', doctype = genshi.output.DocType.HTML)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Page title here</title>
<link href="stylesheet.css" type="text/css" rel="stylesheet">
</head>
<body>
<div>Body content here</div>
</body>
</html>
>>>
>>> # HTML 5
>>> print template.render ('html', doctype = genshi.output.DocType.HTML5)
<!DOCTYPE html>
<html>
<head>
<title>Page title here</title>
<link href="stylesheet.css" type="text/css" rel="stylesheet">
</head>
<body>
<div>Body content here</div>
</body>
</html>

Note how the template library handles not only obvious issues like the doctype and <html> tag, but also small incompatibilities like the closing solidus in the <link> tag.
Text
Text is best for templating web content
Simon Willison argues...
Mon, 15 Oct 2007 by simonw
Rank 1. Average +0.9 ( 7 votes )
Text-based template engines provide better performance. XML engines use an XML parser (usually a DOM based one) which can have significant overhead. Non-XML engines can be a lot faster - Django's template engine uses a single regular expression operation to toxenize a template and consistently beats XML engines in performance benchmarking.
Simon Willison argues...
Mon, 15 Oct 2007 by simonw
Rank 2. Average +0.5 ( 11 votes )
XML template engines are great if you are generating XML, but in Web applications that isn't always the case. HTML is a better format for real-world Web sites than XHTML (less compatibility issues, better browser support), and most applications need to generate plain text formats such as e-mails as well. While XML template systems can be used for non-XML output doing so tends to be quite uncomfortable and can be overly verbose.
Paddy argues...
Mon, 15 Oct 2007 by paddy3118
Rank 3. Average +0.4 ( 5 votes )
I guess by text you mean all text that is not (supposed to be) XML.

I compared the sources shown here text [makotemplates.org] versus XML [genshi.edgewall.org]and I still don't like reading programming language source written in XML format. XML is supposed to be human writable/readable but even with automatic indenting tools I find it is as welcoming as Lisp syntax - which isn't very.

So all I have as an argument is personal taste, but its what I'd use to choose TEXT.

- Paddy.
Richard Jones argues...
Mon, 15 Oct 2007 by richard
Rank 4. Average +0.2 ( 8 votes )
XML-only templating engines only restrict choice and thus cause web developers unnecessary pain.
Because of the obsession with XML too many web developers produce XHTML when it's incredibly poorly-supported (IE in particular treats XHTML as tag soup at best, and at worst doesn't render it at all), whereas the non-XML HTML is very well-supported. XML-only templating engines only restrict choice and thus cause web developers unnecessary pain.
Martijn Faassen argues...
Mon, 15 Oct 2007 by faassen
Rank 5. Average +0.2 ( 4 votes )
Python is text with a grammar. XML is also text with a grammar. Both have rules. The inevitable output for a web page is HTML. This is a third grammar, related to XML but more liberal.

If you believe that templates need to contain a significant amount of Python code, mixing these grammars become harder to understand. Rather mix two grammars (Python and HTML) in a single template than three.
metapundit argues...
Mon, 15 Oct 2007 by metapundit
Rank 6. Average -0.5 ( 2 votes )
Text template languages are usually less verbose - you don't have to have a containing tag for every conditional and loop. It's also a little easier to separate markup from from logic when scanning a text based template - I think programmers typically prefer text-based template systems for this reason. It also helps that text based template engines are typically more performant and therefore don't typically need to be "compiled". While the performance isn't that big a deal it is nice that the engine can be slightly simpler...
Isaac Csandl argues...
Wed, 17 Oct 2007 by nerkles
Rank 7. Average -1.0 ( 2 votes )
Browser preview of un-rendered templates is overrated.
1. Speed. Look at the stats for Genshi or nearly any other vs. Mako. I haven't looked at other languages, but in Python the text-based engines appear to outperform XML.

2. When you are programmatically generating bits of (X)HTML anyway, the "text" systems like Mako are closer to the language the rest of the application is written with (e.g. Python, Ruby, etc.)

Here's a simple example... it may just be down to taste, but look at how you do a simple loop to produce an unordered list:

Genshi:
<ul>
<li py:for="item in items">${item.name}</li>
</ul>

<ul>
<li py:for="item in items">${item.name}</li>
</ul>


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

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

3. Browser preview of un-rendered templates is overrated. Two concepts--one old, one relatively new--obviate the need for this.

First, we have plain HTML+CSS mock-ups, which you should always have a first iteration designed and approved before you even think about building dynamic templates or writing much server-side code.

Second, once you've moved beyond mock-ups to working on a server, you can use live CSS editing with tools like CSSEdit [macrabbit.com]... assuming you generate standards-based, semantic markup, which in this day and age, everybody does, right? ;) I suppose this approach wouldn't work too well if you still use tables for layout and generate tag soup.

4. Did I mention speed? And reduced server CPU load?

[ Added the code tags. - admin ]