Simple HTML template modules?

Albert Brandl albert.brandl at lindeverlag.at
Fri Jan 26 04:13:50 EST 2001


Rayed Al-Rashed wrote:
> I am trying to build CGI programs using Python, and I searched for
> simple HTML templates modules but couldn't find any ... any ideas
> where I can find such module.

You should have a look at HTMLgen. This module provides a class
called TemplateDocument which can be used like this:

#! /usr/local/bin/python
# somescript.cgi

import HTMLgen

doc = HTMLgen.TemplateDocument("template1.html")
doc.substitutions = {"id": "55", "age": "17", "name": "Peter"}
print "content-type: text/html"
print
print str(doc)
#----------------------------------------

template1.html could e.g. be the following file:

<!-- template for use with HTMLgen.TemplateDocument -->
<!-- each variable contained in {...} is replaced with the -->
<!-- corresponding value in doc.substitutions -->

<html>
        <body>
        <table>
                <tr>
                        <td>ID</td>
                        <td>{id}</td>
                </tr>
                <tr>
                        <td>User</td>
                        <td>{name}</td>
                </tr>
                        <td>Age</td>
                        <td>{age}</td>
                </tr>

        </table>
        </body>
</html>

It might pay off to have a look at the other classes of
HTMLgen. You will find an awsome, object-oriented 
interface to many html elements which can speed up
the development enormously.

The documentation for HTMLgen 2.1 can be found at
http://starship.python.net/crew/friedrich/HTMLgen/html/document.html

There exists a newer version, HTMLgen 2.2.2 - have a look at the vaults of 
parnassus at http://www.vex.net/parnassus/

        Enjoy,

                Albert




More information about the Python-list mailing list