Integrating Twisted Matrix with Cheetah

I really love Twisted Matrix' approach to network programming, etc. As a long-time developer of network apps using C, C++, Java, etc., it is a breath of fresh air for a large class of network apps. One thing I'm not too wild about is its sort of "native" web frameworks (woven, or now nevow). I like very simple templating approaches, and as a fan of Velocity in the Java world am drawn to Cheetah (www.cheetahtemplate.org). So my task was to see if the two could be integrated without compromising the core elements of either. Note that I searched around the web quite a bit trying to see if had been done, but couldn't find anything. As it turns out, it was fairly easy to get the basics going. This file has a template base class for the Cheetah templates and a Twisted Matrix resource to invoke the templates: ======== CheetahMapper.py ========= from twisted.web import resource, server from Cheetah.Template import Template class TwistedCheetahTemplate(Template): twrequest = None class CheetahResource(resource.Resource): isLeaf = 1 def __init__(self, filename, registry): self.filename = filename self.registry = registry def render(self, request): tmplt = TwistedCheetahTemplate(file=self.filename) tmplt.twrequest = request request.write(tmplt.respond()) request.finish() return server.NOT_DONE_YET =================================== As the template below will show, the "twrequest" member in the TwistedCheetahTemplate class holds the Twisted Matrix request object. Twisted Matrix is kind enough to pass the full absolute path to the referenced template file in CheetahResource's constructor. To create and execute the web application, in typical Twisted Matrix fashion, use these commands (from the directory in which the above file resides):
mktap web --path=.\webroot --processor=".cht=CheetahMapper.CheetahResource" --port=8880 twistd --file=web.tap
This simple template will be executed and its results returned by pointing your browser at http://127.0.0.1:8880/test.cht: ============ test.cht ============= ## A dummy template <HTML> <HEAD> <TITLE>Test Cheetah Within Twisted</TITLE> </HEAD> <BODY> <B>We've arrived in a Cheetah template!</B> <br>$twrequest.method <br>$twrequest.path <br>$twrequest.args <br>$twrequest.received_headers </BODY> </HTML> =================================== I have no idea if this will help anyone out or not, but I thought I ought to at least post it. My next task is to add the capability for Python code to be executed to fulfill a request and the results be available when a template is executed. I'm sure this is anathema to some, but I frankly quite like asp.net's code-behind model and think I can make a much-simplified flavor of that work within this general approach. FWIW... Donnie
participants (1)
-
Donnie Hale