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] 

John Millikin argues for "XML"
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.

Return to debate