[Twisted-web] Using Cheetah with twisted.web

L. Daniel Burr ldanielburr at mac.com
Tue Feb 14 09:52:32 MST 2006


On Tue, 14 Feb 2006 10:41:24 -0600, Matt Helm <code.name.eric at gmail.com>  
wrote:

> Hello all, I have just recently started working with twisted.web
> and think is great.
>
> I would like to use Cheetah templates with it - should I be
> doing something more complicated than the following?
>

Probably.  Rendering your template in this way will most certainly block  
the reactor, since Cheetah templates don't support Deferreds.  On the  
other hand, if the template is rendering *very* quickly, then this code  
might not block for long enough to actually matter.

> #################
> # alf.rpy
> from twisted.web import resource
> from Cheetah.Template import Template
>
> f = "foo"
> b = ["bar", "baz"]
>
> class Resource(resource.Resource):
>   def render(self, request):
>     return Template(file="alf.tmpl", searchList = [{'f': f, 'b':  
> b}]).__str__()
>
> resource = Resource()
>
> #################
>
>
> Thanks,
> Matt

I'd suggest you do some timing tests, varying the size of the data passed  
to the template, the complexity of the template, etc.  If it turns out  
that rendering the template synchronously blocks the reactor for too long,  
you can look at breaking the rendering up into stages, using  
reactor.callLater, or a separate rendering thread.

Hope this helps,

L. Daniel Burr



More information about the Twisted-web mailing list