( template Module )



Download the      template module

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
$Home    :=   http://www.embl-heidelberg.de/
$Help  :=   /docs/misc/help.html
$Help_1   :=   /docs/misc/help1.html
$Help_2   :=   /docs/misc/help2.html
$Help_3   :=  /docs/misc/help3.html

$bgColor :=   /docs/images/greenwhite_paper.gif

FirstPage:[
:Commit
  Content-type: text/html
 

   <HTML><BODY BACKGROUND=$bgColor>
   <TITLE> Available Databases </TITLE>
   <FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION=$cgiScript>
   <INPUT TYPE=HIDDEN NAME=fun VALUE=myFirstFun>
   <INPUT TYPE=HIDDEN NAME=USERID VALUE=%s>
   <INPUT TYPE=HIDDEN NAME=FILENAME VALUE=%s><P>
   <CENTER> <H1> The Big  Shopper </H1> </CENTER>
   <HR> <P><CENTER>
   <TABLE BORDER=2>
          <TR> <TH COLSPAN=2> Product Selection </TH>
:select_start
    <TR> <TD> 
    <B> %s </B> <SELECT NAME=%s
:Option
    <OPTION VALUE=%s %s> %s
:select_end
    </SELECT>
    </TD>
    <TD><A HREF=Help_%s>More Info</A>
    </TR>
:tableEnd
   </TABLE>
   </CENTER>
   <INPUT TYPE=SUBMIT NAME=SUBMIT VALUE="SUBMIT">
   <HR>
:manufHome
   <A HREF=%s>%s</A> |

:htmlTail
   <HR> <P>
   <B><A HREF=$Home>Back to Home</A></B>
   </FORM>
   </BODY>
   </HTML>
]
 

secondPage:[
:htmlHead
   <HTML><BODY><TITLE>  Chosen Lists </TITLE>
   <CENTER> <H1> The Big  Shopper </H1> </CENTER>
   <H2> The following items are chosen </H2>

:chosen
   <B> %5d )</B> Item <B> %s </B>  Manufacturer  <A HREF=%s>%s</A>  <BR>

:cost
  <HR>Total Cost :  %d <HR>
:htmlTail
   </BODY>   </HTML>

]

 

#!/usr/pub/bin/python

###############################################################
#
#
#   Uses the  html template 
#
#
#
#

import sys
from template import *

manuf = ['Adobe Systems','Berkley Systems','Compaq Computer',
         'Coral Systems','Digital Equip Corp','IBM','Matrox Graphics']

manufHome = ['www.adobe.com','www.berksys.com','www.compaq.com',
             'www.corel.com','www.digital.com','www.ibm.com',
             'www.matrox.com']

items = ['Monitor','keyboard','CD-ROM Drive','Floppy Drive','mouse','ALL']

selected = 'IBM'

t = template()
t.TemplateInit('/home/ramu/py/sales.tmpl',None)
t.TemplateSetPage('FirstPage')
t.TemplatePrintFrom('Commit','voila','None')
t.TemplatePrintFrom('select_start',' Choose Your Maker','prodName')

for i in manuf:
    if i == selected:
       t.TemplatePrintFrom('Option',i,'SELECTED',i)
    else:
        t.TemplatePrintFrom('Option',i,'',i)
t.TemplatePrintFrom('select_end','1')

selected = 'monitor'
t.TemplatePrintFrom('select_start',' Choose the device ','prodName')

for i in items:
    if i == selected:
        t.TemplatePrintFrom('Option',i,'SELECTED',i)
    else:
        t.TemplatePrintFrom('Option',i,'',i)
t.TemplatePrintFrom('select_end','2')
t.TemplatePrintFrom('tableEnd')

j=0
for i in manuf:
    t.TemplatePrintFrom('manufHome',manufHome[j],i)
    j = j + 1

t.TemplatePrintFrom('htmlTail')

#
#  Assume you have already chosen through cgi interface
#

t.TemplateSetPage('secondPage')
t.TemplatePrintFrom('htmlHead')

j = 0

for i in items:
    t.TemplatePrintFrom('chosen',j+1,i,manufHome[j],manuf[j])
    j = j + 1
t.TemplatePrintFrom('htmlTail')

 

The output

Content-type: text/html
 

<HTML>
<body background=/docs/images/greenwhite_paper.gif>
<TITLE> Available Databases </TITLE>
<FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION=/cgi-bin/misc/samplewww>
<INPUT TYPE=HIDDEN NAME=fun VALUE=myFirstFun>
<input type=hidden name=userId value=voila>
<input type=hidden name=fileName value=None>
<P>
<CENTER> <H1> The Big  Shopper </H1> </CENTER>
<HR> <P>
<CENTER>
<TABLE BORDER=2>
<TR> <TH COLSPAN=2> Product Selection </TH>
<TR> <TD>
<B>  Choose Your Maker </B> <SELECT NAME=prodName>
<OPTION VALUE=Adobe Systems > Adobe Systems
<OPTION VALUE=Berkley Systems > Berkley Systems
<OPTION VALUE=Compaq Computer > Compaq Computer
<OPTION VALUE=Coral Systems > Coral Systems
<OPTION VALUE=Digital Equip Corp > Digital Equip Corp
<OPTION VALUE=IBM SELECTED> IBM
<OPTION VALUE=Matrox Graphics > Matrox Graphics
</SELECT>
</TD>
<TD><A HREF=Help_1>More Info</A>
</TR>
<TR> <TD>
<B>  Choose the device  </B> <SELECT NAME=prodName>
<OPTION VALUE=Monitor > Monitor
<OPTION VALUE=keyboard > keyboard
<OPTION VALUE=CD-ROM Drive > CD-ROM Drive
<OPTION VALUE=Floppy Drive > Floppy Drive
<OPTION VALUE=mouse > mouse
<OPTION VALUE=ALL > ALL
</SELECT>
</TD>
<TD><A HREF=Help_2>More Info</A>
</TR>
</TABLE>
</CENTER>
<input type=submit name=submit value="Submit">
<HR>
<A HREF=www.adobe.com>Adobe Systems</A> |
<A HREF=www.berksys.com>Berkley Systems</A> |
<A HREF=www.compaq.com>Compaq Computer</A> |
<A HREF=www.corel.com>Coral Systems</A> |
<A HREF=www.digital.com>Digital Equip Corp</A> |
<A HREF=www.ibm.com>IBM</A> |
<A HREF=www.matrox.com>Matrox Graphics</A> |
<HR> <P>
<B><A HREF=http://www.embl-heidelberg.de/>Back to Home</A></B>
</FORM>
</BODY>
</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


Reference:
Generating dynamic documents from template
Digital Systems Report, Summer,1998, Page(14-19)

 Home