Use Cheetah Templates with Google App Engine

Submitted by jeff on Mon, 05/26/2008 - 08:53.

Community help for Google App Engine has 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 command:
python setup.py install --install-lib /path/to/myapp/

Running the command above compiled and installed Cheetah templates into a new Cheetah subdirectory of my single Google App Engine application. For Mac it was something like /Users/bart/myapp/Cheetah.

Now, in your code file you can import Cheetah! In the Google example helloworld.py I would put this line near the top with the other includes

from Cheetah.Template import Template

Then, to actually have a Cheetah Template return something, you would add something like this to the MainPage class to output the rendered template.

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
        self.response.out.write(tmpl)

My template might be this:

$user is using Cheetah

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

Jeff is using Cheetah

Notes

If you have more applications you probably need to copy or re-install the Cheetah directory into each different application. You need to do this because the applications get deployed to Google.

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. 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 should explain that I do not like the Django templating system. I have gotten too used to Cheetah. Django’s hardline stance is to separate logic from presentation. Yes, the Django approach works for many but I just want to make my own fricking little app and I like running loops and instantiating usable variables within my templates, a luxury afforded by Cheetah.

Maybe in the future I will learn how to use a different app Framework instead of Google’s default webapp Framework.


Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
13 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.