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]
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>
>>> # 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>