However, this can also be done with XML and XSL. I personally think that using python types directly, and then writing CHTL masks is much easier and straightforward than XML/XSL, but there might be several reasons why you'd want to use XML/XSL:
Also, several other XML/XSL modules are available for Python, and using them is also probably very easy.
Start by downloading 4Suite from http://4suite.org and installing it on your machine.
You can test your installation by firing up the Python interpreter and typing:
>>> from Ft.Xml.Xslt.Processor import Processor ::: Using pDomlette >>>
Once you have 4Suite installed, is is very easy to use it from CherryPy. You can use regular masks to write your XSL stylesheets, and you have several ways to generate your XML (using a function, a view or even a mask).
The following example is a simple example that demonstrates a basic XML/XSL transformation:
from Ft.Xml.Xslt.Processor import Processor CherryClass XslTransform: function: def transform(self, xslStylesheet, xmlInput): processor = Processor() processor.appendStylesheetString(xslStylesheet) return processor.runString(xmlInput, 0, {}) CherryClass Root: view: def index(self): return xslTransform.transform(self.xslStylesheet(), self.xmlInput()) mask: def xslStylesheet(self): <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body></html> </xsl:template> </xsl:stylesheet> def xmlInput(self): <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> </cd> </catalog>
How does it work ?
Here is the HTML document generated in this example:
<html> <body> <h2>My CD Collection</h2> <table border='1'> <tr bgcolor='#9acd32'> <th align='left'>Title</th> <th align='left'>Artist</th> </tr> <tr> <td>Empire Burlesque</td> <td>Bob Dylan</td> </tr> <tr> <td>Hide your heart</td> <td>Bonnie Tyler</td> </tr> </table> </body> </html>
PS: Note that it seems that the 4Suite API has changed again in the latest releases, so you might have to tweak the above code if you're using a recent release.
See About this document... for information on suggesting changes.