Simple HTML template modules?
Walter Dörwald
doerwalter at my-deja.com
Fri Jan 26 12:52:45 EST 2001
In article <94rev7$ol4$1 at readme.inode.at>,
Albert Brandl <albert.brandl at lindeverlag.at> wrote:
> 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>
>
> [...]
Alternatively you might have a look at XIST (available from
ftp://titan.bnbt.de/pub/livinglogic/xist/), where Alberts example
looks like this:
from xist import html
id = 55
age = 17
name = "peter"
e = html.html(
html.body(
html.table(
html.tr(html.td("ID"), html.td(id)),
html.tr(html.td("User"), html.td(name)),
html.tr(html.td("Age"), html.td(age))
)
)
)
print "Content-Type: text/html\n"
print e.asBytes(encoding="ascii")
XIST is XML based and is made to be easily extensible
with new functionality. (But note that I'm biased,
as I wrote XIST)
Bye,
Walter Dörwald
Sent via Deja.com
http://www.deja.com/
More information about the Python-list
mailing list