zachary.com

personal pages

All ad proceeds donated to charity.

Quixote and Cheetah

I've been playing with Quixote, a Python-based web application framework a bit for a personal project. Among other things, I like the simplicity of the basic concepts. At its heart, it "just" maps from URLs to Python callable objects. There is other structure available, but the various pieces seem to nicely stay out of the way if you don't need them.

As a quick experiment, I created a simple class to add Cheetah template support. This class is simplified from an example of linking Quixote with another template system for Python, SimpleTAL.

from Cheetah.Template import Template as CheetahTemplate

class Template(object):

    _q_exports=[]

    def __init__(self, path):
        self._template = CheetahTemplate( file=path )
        
    def _makeEnvironment(self, request):
        t = self._template
        t.cgi = request.environ
        t.form = request.form
        t.session = request.session

    def __call__(self, request):
        request.response.set_header('Cache-control', 'no-cache')
        self._makeEnvironment(request)
        return str( self._template )

    _q_index=__call__

***
highlight file error
***

That's it! You can stuff nearly anything you want in the _makeEnvirontment method to setup the Cheetah namespace. The __init__ method will load, compile and cache the compiled template. The _q_exports and _q_index attributes are sufficient to implement Quixote's callable protocol. In my Quixote published namespace, I can then just do this:

home = Template( 'home.tmpl' )
_q_exports= [ 'home' ]

***
highlight file error
***

to use a template.

Categories: python


This page last modified Tuesday 24 May, 2005 by David Creemer
All content Copyright 2003-2005, David Z Creemer