Latest Version: 0.9.6.2
  Dashboard > Pylons Cookbook > ... > Templating > Cheetah templates
  Pylons Cookbook Log In | Sign Up   View a printable version of the current page.  
  Cheetah templates
Added by Mike Orr, last edited by Noah Tye on Jul 25, 2007  (view change) show comment
Labels: 
(None)

Author: Mike Orr

Installation

For best results use Pylons >= 0.9.5 with Cheetah >= 2.0rc8. Neither of these are released as of March 2007, but you can install the development versions in the meantime:

$ easy_install Pylons==dev
$ cvs -d:pserver:anonymous@cheetahtemplate.cvs.sourceforge.net:/cvsroot/cheetahtemplate login
$ cvs -z3 -d:pserver:anonymous@cheetahtemplate.cvs.sourceforge.net:/cvsroot/cheetahtemplate co -P Cheetah
$ easy_install ./Cheetah

Advantages of Pylons 0.9.5:

  • "paster make-config" no longer depends on obsolete Cheetah 1.0.

Advantages of Cheetah 2.0rc8:

  • Better Unicode handling.

You'll also need the TurboCheetah package ("easy_install TurboCheetah"). Change the last line in turbocheetah/cheetahsupport.py ('return str(tempobj)') to:

if fragment:
    return tempobj.fragment()
else:
    return tempobj.respond()

This not only works better with Unicode, it gives some support for "fragments" (see below).

Application configuration

In your application's middleware file (myapp/config/middleware.py) after the config.init_app() call, add this line:

config.add_template_engine("cheetah", "myapp.templates", {})

Your controller methods will then look like this:

def hello(self):
    c.what = "Cheetah world"
    return render_response("cheetah", "index")

The template for this controller method would be myapp/templates/index.tmpl, containing "Hello, ${c.what}!"

Important: Your templates directory and any subdirectories must contain an __init__.py file, or TurboCheetah will not find the templates.

To avoid the "cheetah" argument in render_response, make Cheetah the default template. Do this by adding "config.template_engines = []" before the add_template_engine line in middleware.py. The first engine added is the default. Then your controller method can say 'return render_response("index")'.

Fragments

If you pass 'fragment=True' to render_response, the template is supposed to be rendered without any site decorations (i.e., inherited templates). Cheetah doesn't have this concept, but if you define a '#def fragment' method in your template, TurboCheetah will call that and use it as the fragment.

As you would probably like to take advantage of Cheetah's inheritance, you may refer to your mybasetemplate with

1
#extends myapp.templates.mybasetemplate

It's worth mentioning that Pylons (as of 0.9.5) won't take care to compile .tmpl files into .py classes, it's up to the developer to `cheetah-compile` every .tmpl or rely on tools as Make (as suggested here).
Both could safe time from googling for <type 'exceptions.ImportError'>: No module named mybasetemplate errors.

Posted by Daniele Paolella at Jul 24, 2007 19:26 | Permalink
Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top