<br><br><div><span class="gmail_quote">On 2/22/06, <b class="gmail_sendername">Todd Thomas</b> &lt;<a href="mailto:caliban19@gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">caliban19@gmail.com
</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><span><br><br><div><span class="gmail_quote">On 2/22/06, <b class="gmail_sendername">Matt Helm</b> &lt;<a href="mailto:code.name.eric@gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">

code.name.eric@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 2/21/06, Graeme Glass wrote:<br>&gt; Would you mind posting some sample code, for refrence?<br>&gt; Many thanks.<br><br>class Resource(resource.Resource):<br><br>&nbsp;&nbsp; def render(self, request):<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def cheeto(r):
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.write(r.__str__())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.finish()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def cherr(r):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.write(&quot;Bang!&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.write(str(r))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.finish()<br><br>


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d_cheeto = threads.deferToThread(Template, file=&quot;alf.tmpl&quot;, searchList =<br> slist)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d_cheeto.addCallback(cheeto)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d_cheeto.addErrback(cherr)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return server.NOT_DONE_YET<br>


<br>resource = Resource()<br><br>_______________________________________________<br>Twisted-web mailing list<br><a href="mailto:Twisted-web@twistedmatrix.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">

Twisted-web@twistedmatrix.com</a><br><a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web</a><br></blockquote></div></span></div>webexp.py<br>
--------------------------------------------<br>
from twisted.application import internet, service<br>
from twisted.web import static, server, script<br>
import CheetahMapper<br>
<br>
root = static.File(&quot;./&quot;)<br>
root.indexNames=['index.cht']<br>
root.ignoreExt(&quot;.cht&quot;)<br>
root.processors = {'.cht': CheetahMapper.CheetahResource}<br>
application = service.Application('web')<br>
sc = service.IServiceCollection(application)<br>
site = server.Site(root)<br>
i = internet.TCPServer(8888, site)<br>
i.setServiceParent(sc)<br>
<br>
CheetahMapper.py<br>
------------------------------------------------<br>
from twisted.web import resource, server<span><br>
from Cheetah.Template import Template<br>
<br>
<br></span>
class CheetahResource(resource.Resource):<br>
&nbsp;&nbsp;&nbsp; isLeaf = 1<br>
<br>
&nbsp;&nbsp;&nbsp; def __init__(self, filename, registry):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.filename = filename<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.registry = registry<br>
<br>
&nbsp;&nbsp;&nbsp; def render(self, request):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tmplt = Template.compile(file=self.filename)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inst=tmplt()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.write(str(inst))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; request.finish()<span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return server.NOT_DONE_YET<br>
<br></span>
time.cht<br>
------------------------------------------------------<br>
#import time<br>
&lt;html&gt;<br>
&lt;head&gt;<br>
&lt;title&gt;Time Example&lt;/title&gt;<br>
&lt;/head&gt;<br>
&lt;body&gt;<br>
It is $time.strftime(&quot;%I:%M %p&quot;).<br>
&lt;br&gt;<br>
Date is $time.strftime(&quot;%A, %B %D, %Y&quot;).<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>
-----------------------------------------------------------------------<br>
Not the best code in the world, but it has been useful for me for just throwing together quick pages and experimenting.<br>
<br>
ToddB<br>

</blockquote></div>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.<br>
<br>
ToddB<br>