spyce
home
license
community
download
examples
resources
wishlist
contrib (@sf)
documentation
intro
lang
runtime
modules
tags
install
exits
sourceforge
statistics
freshmeat

Documentation - Modules
[[ Spyce ]]
Python Server Pages
by Rimon Barr

Prev: 4.12 - Pool Up: 4 - Modules Next: 4.14 - Compress

4.13. Template

In general, a template is useful for separating form from function. Or, in other words, one would like web page designers to play with one file, and programmers to play with another, so that they don't step on each other's toes. A templating engine then puts the two pieces (template and data) together to create the final output. The Spyce language internally provides Spyce lambdas, which can be very useful for templating purposes. This module provides hooks to various external templating engines.

Spyce interacts with the rather powerful Cheetah Python-based templating engine. The Cheetah engine is not included with the Spyce distribution, some recommended installation instructions are provided below. The Cheetah engine is invoked as follows:

  • cheetah( file, [lookup] ):
    Calling this function will invoke the Cheetah engine to compile (and cache) the template file provided. The engine then "runs" the template and fills in the appropriate data from the lookup dictionary, or list of dictionaries. If the lookup is omitted, the convenient default is to use the local and global variables from the current context. The template is filled and the resulting string is returned.

To install Cheetah (instructions correct as of version 0.9.15a1), follow the following steps:

  • Download the latest Cheetah engine from their website.
  • Extract the files from the gzipped tarball into some directory
  • Switch to root user
  • In that directory type: python setup.py install
  • Now, change directory to: /usr/lib/python2.2/site-packages/
  • Type in: chmod -R a+r Cheetah*
  • Type in: chmod a+x `find Cheetah -type d`

In general, that the Python path must simply include the Cheetah installation directory and Spyce will find it. If not, you will see an import error. At this time, the Cheetah engine requires Python version 2.0 or higher.

Support for other templating engines will be added as needed. An example of how templates are used is shown below, with the template files appended thereafter.

examples/template.spy
[[.import name=template]]
[[import sys]]
<html><body>
The template module interfaces with various templating
engines. <br>
It currently supports: 
  <a href="http://www.cheetahtemplate.org">Cheetah</a>
<hr>

[[
  persona = 'world'
  num = 10
]]
<b>Cheetah template:</b><br>
[[ try: { ]]
  [[=template.cheetah('template.tmpl')]]
[[ } except ImportError: { ]]
  Unable to import Cheetah.Compiler from path=[[=sys.path]]
  <br><b>The Cheetah is likely not (properly) installed.</b>
[[ } ]]
<p>

</body></html>
Run this code.
(requires Spyce-enabled web server)

examples/template.tmpl
Hello $persona!
#for i in range($num)
$i #slurp
#end for
Run this code.
(requires Spyce-enabled web server)


Prev: 4.12 - Pool Up: 4 - Modules Next: 4.14 - Compress


© 2002-05 Rimon Barr
email: rimon@acm.org
Spyce Powered SourceForge Logo [[ Spyce ]]
Python Server Pages
version 1.3.13