Generating dynamic documents from templates with python (useful for html)
Most of the cgi programs have the html
text within themselves, thus making
cgi programs larger, less comfortable
to view, difficult to go through the code.
The ideal situation would be separating
html text from the program.
The Template file
The template file (html text) can have
variables, page labels,
and paragraph labels. Recursive substitution
of the variables are
possible but not supported in this version.
Any line starts with '#'
character
is treated as comment line and
it is simply ignored.
Within this template file a variable can
be set like
| $cgiScript := /cgi-bin/misc/samplewww
$Home := http://www.embl-heidelberg.de/ |
and can be inserted afterwards in the text
other than page labels and
paragraph labels.
Page label consists of a word, immediately
followed by a colon and
left angle brace for example
firstPage:[
Within a page paragraphs starts with a colon and a word for example
:firstPara
There can be any number of paragraphs
within a page.
The page should end with a left
brace ']'
There can be any number of pages in
a template file.
Format strings are supported to build dynamic html pages.
Check sales.tmpl template file below.
The Template Module
The template.py module has a class
called template and several class methods.
Most of the time you may need only three
methods to use,
TemplateInit, TemplateSetPage and
TemplatePrintFrom.
Do not use the TemplateEvalPage method. This is for advanced
users
who wants to have the functions inserted in the template file and evaluate
on
run time. Feel free to ask me if you are interested! You
need to have
additional two modules.
First import the module with a python statement
| from template import
*
t = template() t.TemplateInit('sales.tmpl','firstPage') |
Once
the desired page is set, you can start printing the needed
paragraphs with TemplatePrintFrom,
like
t.TemplatePrintFrom('htmlHead')
The TemplatePrintFrom
method
can have variable length argument.
The number of arguments and their type
should match according to
the format string used under the paragraph
label.
For example, if the format string of the
paragraph in the template file is
| :simple
ones %s twos %s five = %d |
Then the TemplatePrintFrom statement
|
t.TemplatePrintFrom('simple','are many','are not so many',5) |
The first parameter in the
TemplatePrintFrom
is the paragraph name, rest
of the parameters are
passed to the format string.
Obviously number of parameters passed should match with the format
string modifiers. When it does not
match, you get a detailed error report about what went wrong !
The output would be
|
ones are many twos are not so many five = 5 |
The method TemplateResetPage()resets
to the last page. So you can
share common paragraphs back and forth.
The default output is standard output.
Output can be redirected
to files with TemplateRedirectPrint
method.
It can be reset to
standard out by TemplateResetPrint
method.
One another important method is TemplateFillFrom.
This returns the
string instead of printing it. This can
be very useful when you want to make a system
call, running a shell script etc.,
The TempateCheck,
TemplateShowVariables
methods are
kept for debugging
purpose.
| sales.tmpl The Templae File | sales.py CGI program shows how to use sales.tmpl template |
| ####################################################
# # html template file # $cgiScript:=
/cgi-bin/misc/samplewww
$bgColor := /docs/images/greenwhite_paper.gif FirstPage:[
<HTML><BODY BACKGROUND=$bgColor>
:htmlTail
secondPage:[
:chosen
:cost
]
|
#!/usr/pub/bin/python
###############################################################
import sys
manuf = ['Adobe Systems','Berkley Systems','Compaq
Computer',
manufHome = ['www.adobe.com','www.berksys.com','www.compaq.com',
items = ['Monitor','keyboard','CD-ROM Drive','Floppy Drive','mouse','ALL'] selected = 'IBM' t = template()
for i in manuf:
selected = 'monitor'
for i in items:
j=0
t.TemplatePrintFrom('htmlTail') #
t.TemplateSetPage('secondPage')
j = 0 for i in items:
|
The output |
|
|
Content-type: text/html <HTML>
|
|
| <HTML>
<CENTER> <H1> The Big Shopper </H1> </CENTER> <H2> The following items are chosen </H2> <B> 1 )</B> Item <B> Monitor </B> Manufacturer <A HREF=www.adobe.com>Adobe Systems</A> <BR> <B> 2 )</B> Item <B> keyboard </B> Manufacturer <A HREF=www.berksys.com>Berkley Systems</A> <BR> <B> 3 )</B> Item <B> CD-ROM Drive </B> Manufacturer <A HREF=www.compaq.com>Compaq Computer</A> <BR> <B> 4 )</B> Item <B> Floppy Drive </B> Manufacturer <A HREF=www.corel.com>Coral Systems</A> <BR> <B> 5 )</B> Item <B> mouse </B> Manufacturer <A HREF=www.digital.com>Digital Equip Corp</A> <BR> <B> 6 )</B> Item <B> ALL </B> Manufacturer <A HREF=www.ibm.com>IBM</A> <BR> |
|
Your suggestions are most welcome
See also the cgi
made easy