Peter's Blog 9.3.2004

2004-03-09

Rejigged website generation.

Rejigged website generation. Decided to try using Cheetah (www.cheetahtemplate.org), a python templating library. This allows me to modify my html template, putting in variable names to be replaced by code. It was very easy to get going and it looks very powerful: you can virtually embed python code in your html.

Example:

#
from Cheetah.Template import Template

strTemplate = """<body>
                A $simple example.<br/>
                 Hello $names['wibble'], how are you today?<br/>
                 $MYGOD( 'software')
                 <table>
                 #for oEntry in $ENTRIES
                 <tr><td>$oEntry['Title']</td><td>$oEntry['Date']</td></tr>
                 #end for
                 </table>
        </body>"""

def MyGod( strPara):
        return "Now that's what I call " + strPara

oEntries = [ { 'Title': 'Fred', 'Date': 'Now'},
                                { 'Title': 'Noreen', 'Date': 'blub'}
                        ]

oNames = { 'wibble': 'Peter' }

oSearchList = { 
                'simple': "Simple",
                'names': oNames,
                'MYGOD': MyGod,
                'ENTRIES': oEntries
                }

strHTML = Template( strTemplate, searchList=[oSearchList])

print strHTM

Generates:

<body>
        A Simple example.<br/>
         Hello Peter, how are you today?<br/>
         Now that's what I call software
         <table>
         <tr><td>Fred</td><td>Now</td></tr>
         <tr><td>Noreen</td><td>blub</td></tr>
         </table>
</body>

The advantage of all this complexity over strHTML.replace( '$simple', strSimple) is in the way that it allows loops to be expanded. The system as it stands allows me to create the HTML and make it pretty using HTML-Kit, then use python to populate it up. My python script has no layout HTML in it. The only other HTML involved is that generated from the DayNotez entries using DocUtils.

posted at 14:41:04    #    comment []    trackback []
March 2004
MoTuWeThFrSaSu
1 2 3 4 5 6 7
8 91011121314
15161718192021
22232425262728
293031    
Feb
2004
 Apr
2004

A blog documenting Peter's dabblings with Python, Gentoo Linux and any other cool toys he comes across.

XML-Image Letterimage

© 2004, Peter Wilkinson

Bisi and me