[Twisted-web] Using Cheetah with twisted.web

Todd Thomas caliban19 at gmail.com
Wed Feb 22 22:23:03 MST 2006


On 2/22/06, Todd Thomas <caliban19 at gmail.com> wrote:
>
>
>
> On 2/22/06, Matt Helm < code.name.eric at gmail.com> wrote:
> >
> > On 2/21/06, Graeme Glass wrote:
> > > Would you mind posting some sample code, for refrence?
> > > Many thanks.
> >
> > class Resource(resource.Resource):
> >
> >    def render(self, request):
> >
> >        def cheeto(r):
> >            request.write(r.__str__())
> >            request.finish()
> >
> >        def cherr(r):
> >            request.write("Bang!")
> >            request.write(str(r))
> >            request.finish()
> >
> >        d_cheeto = threads.deferToThread(Template, file="alf.tmpl",
> > searchList =
> > slist)
> >        d_cheeto.addCallback(cheeto)
> >        d_cheeto.addErrback(cherr)
> >
> >        return server.NOT_DONE_YET
> >
> > resource = Resource()
> >
> > _______________________________________________
> > Twisted-web mailing list
> > Twisted-web at twistedmatrix.com
> > http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
> >
> webexp.py
> --------------------------------------------
> from twisted.application import internet, service
> from twisted.web import static, server, script
> import CheetahMapper
>
> root = static.File("./")
> root.indexNames=['index.cht']
> root.ignoreExt(".cht")
> root.processors = {'.cht': CheetahMapper.CheetahResource}
> application = service.Application('web')
> sc = service.IServiceCollection(application)
> site = server.Site(root)
> i = internet.TCPServer(8888, site)
> i.setServiceParent(sc)
>
> CheetahMapper.py
> ------------------------------------------------
> from twisted.web import resource, server
> from Cheetah.Template import Template
>
>
> class CheetahResource(resource.Resource):
>     isLeaf = 1
>
>     def __init__(self, filename, registry):
>         self.filename = filename
>         self.registry = registry
>
>     def render(self, request):
>         tmplt = Template.compile(file=self.filename)
>         inst=tmplt()
>         request.write(str(inst))
>         request.finish()
>         return server.NOT_DONE_YET
>
> time.cht
> ------------------------------------------------------
> #import time
> <html>
> <head>
> <title>Time Example</title>
> </head>
> <body>
> It is $time.strftime("%I:%M %p").
> <br>
> Date is $time.strftime("%A, %B %D, %Y").
> </body>
> </html>
> -----------------------------------------------------------------------
> Not the best code in the world, but it has been useful for me for just
> throwing together quick pages and experimenting.
>
> ToddB
>
There is a catch22 with cheetah and twisted that just annoys me. Cheetah has
an excellent caching mechanism built in, which would be awesome to use for
deferreds which access databases. If you basically use Cheetah via namespace
there is no way to use this caching. You could wrap cheetah class in a
deferred, then call deferred from within the template but this seems like
overkill, wish there was a way to see if result is cached then call deferred
if needed, so far I haven't found an obvious way for this to work.

ToddB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20060222/cfd2ff48/attachment.htm


More information about the Twisted-web mailing list