Use Cheetah Templates with Google App Engine

Community help for Google App Engine has been less than stellar because nobody takes a moment to explain how to do the supposedly “basic” python stuff. It took me a long time but I finally figured out the process to install Cheetah Templates for use with Google App Engine.

Here is what I had to do on Mac OS X 10.5:

  • run the install command:
python install —install-lib /path/to/myapp/

Running the command above compiled and installed Cheetah templates into a new Cheetah subdirectory within my single Google App Engine application. For Mac the install path was similar to /Users/bart/myapp/Cheetah.

Now, you can import Cheetah in your Google App Engine project! In the Google Documentation example I would put the Cheetah include line near the top of the file with the other includes

from Cheetah.Template import Template

Then, to actually have a Cheetah Template return something, you would use something like this to the MainPage class to output the rendered template. This is a more complete example.

from Cheetah.Template import Template #woohoo! Cheetah!
import os
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.api import users
class MainHandler(webapp.RequestHandler):
def get(self):
        #find the desired file
        path = os.path.join(os.path.dirname(file), ‘templates/cheetahtest.tmpl’)
        user = users.get_current_user()
        template_values = { ‘user’:user,}
        #render template with values inserted
        tmpl = Template( file = path, searchList = (template_values,) )
        #output the template to screen

My template (i.e. templates/cheetahtest.tmpl) might contain this:

<p>$user is using Cheetah</p>

Let us assume that the logged in user is ‘Jeff’ for sample output. The rendered output would be:

<p>Jeff is using Cheetah</p>


If you have more applications you need to copy or re-install the Cheetah directory into each different application individually. You need to do this because the applications get deployed to Google servers and will not have access to each others’ libraries.

Google says that App Engine works with “Pure Python” extensions and the process is simply to upload your extensions with the app. Since I put the install in /path/to/myapp/Cheetah I presume you can put other extensions and/or frameworks in there as well. Just download the Python source and configure/compile them to go into the app’s folder.

Why Cheetah?

Why did I go through this? First, I do not like the Django templating system. I have gotten accustomed to Cheetah because I use it at work. Second, Django’s hardline stance is to separate logic from presentation. Yes, the Django approach works for many but I like running loops and instantiating usable variables within my templates, a luxury afforded by Cheetah. Finally, Cheetah’s syntax is much closer to regular Python than the Django template syntax.