Thursday, May 22, 2008

Play with Cheetah : the python based templating engine

Cheetah is a python powered  Templating engine and code generation tool (Though I never tried it’s code generation functionality). Its main application is in the Web Development scenario itself.

To start with Cheetah, we can download the latest release of cheetah from http://sourceforge.net/project/showfiles.php?group_id=28961.
Installation is done by the usual
python setup.py install

After this we are going to test the prime functionality of Cheetah, the Web Development scenario.

The simplest template (Greeting.tmpl)
###################################
Hello, $firstName.

Your order (#$order) has shipped:
###################################                                  

Compile it using Cheetah

cheetah compile Greeting.tmpl
Compiling Greeting.tmpl -> Greeting.py

Now we got a Greeting.py. So let’s  test that in python

>>> from Greeting import *
>>> t = Greeting()
>>> t.firstName= ‘Maxin’
>>> t.order = 23342
>>> print t
Hello, Maxin.

Your order (#23342) has shipped:

Hmm.. It works..
Best reference for cheetah is available at http://www.cheetahtemplate.org/docs/users_guide_html

Posted by maxinbjohn at 07:28:04 | Permalink | Comments (2)