Login to your account |
Other Options |

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]
Enter the chair-text in the area below.
5 comments
The ability to preview templates directly in a browser shouldn't be minimised!
<h1 template:replace="title">This will be the Headline</h1>
appealing for the sake of consistency
(X)HTML based templates allow you to split work between web designers and programmers.
XHTML templates are clearer than text-based ones.
<ul> % for item in items: <li>${item.name}</li> % endfor </ul>
<ul> <li tal:repeat="items" tal:content="item/name">Item Name</li> </ul>
<?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>
>>> # 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>
XML-only templating engines only restrict choice and thus cause web developers unnecessary pain.
Browser preview of un-rendered templates is overrated.
<ul> <li py:for="item in items">${item.name}</li> </ul>
<ul> % for item in items: <li>${item.name}</li> % endfor </ul>