[Twisted-web] Using Cheetah with twisted.web

Todd Thomas caliban19 at gmail.com
Wed Feb 22 21:59:33 MST 2006


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20060222/e7d1fd7c/attachment.htm


More information about the Twisted-web mailing list