1.12.1 Cheetah Template

##
1.12.1 Cheetah Template

Cheetah is a powerful, stable and well documented templating system. It works by parsing the template into a Python script and then executing that script with the dictionary to produce output. The performance of Cheetah can be improved by writing this script to a file and executing it each time Cheetah is run rather than re-generating it every time.

The useCompiled parameter of the `parse()` function can be used to determine the behaviour of this compilation. If useCompiled is `False` the template is parsed every time. This is the slowest but simplest option. If useCompiled is `True` the compiled template is used even if the original template has changed. This is the fastest option but you must manually tell Cheetah to recompile the template if it changes. If useCompiled is `'auto'` then Cheetah will use the compiled file as long as the template has not been modified. If it has it will automatically recompile the template.

Warning: This is the best comprimise. If useCompiled is `True` or `'auto'` then Cheetah must have write access to the directory containing the templates. If it doesn't you may get Internal Server Errors, particularly if you are using `web.error` with Cheetah templates to catch errors as an error will be thrown in the error catching code and this will lead to an error that is hard to track down.

You can also use Cheetah directly by importing it as follows:

    import web
    import Cheetah

Here is an example Cheetah template:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>$title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <h1>$title</h1>
    $welcomeMessage
    #if $testVar == True
    The test variable is True
    #else
    The test variable is not True
    #end if
    </body>
    </html>

Here is a program to manipulate it:

    #!/usr/bin/env python

    import sys; sys.path.append('../../../') # show python where the web modules are
    import web.template

    dict =  {
        'welcomeMessage':'Welcome to the test page!',
        'testVar':True,
        'title':'Cheetah Example',
    }

    print web.template.parse(
        type='cheetah',
        file='file-web-template-cheetah.tmpl',
        dict=dict
    )

And here is the output produced:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Cheetah Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <h1>Cheetah Example</h1>
    Welcome to the test page!
    The test variable is True
    </body>
    </html>

See Also:

Cheetah Template Homepage
  The Cheetah homepage has full documentation for using Cheeath and explains the full syntax available and the range of options that can be used.