Vinj Vinj wrote:
>I'm setup to use quixote with twsited and would like
>to use zpt (simpletal implementation). I had the
>following line which worked with webware but fails in
>quixote:
>
>return (template.expand (context, response))
>
>I guess simpletal is setup to expect a stream(file)
>like object which the response object is not.
>
>I looked at the publish code and though I had a
>workaround, except that the some of the functions in
>publish are over written by twisted_http.
>
One answer is to write to a StringIO object, there may be others.
As it happens, I was playing with the same combination just the other
day. Below is a small class I wrote which compiles templates, caches
them and makes rendering the template trivial.
from simpletal import simpleTAL, simpleTALES
from cStringIO import StringIO
import os.path
class TemplateRenderer:
def __init__(self):
self.cache = {}
def getTemplate(self, filename):
mtime = os.path.getmtime(filename)
template = self.cache.get(filename,None)
if template and template[1] == mtime:
return template[0]
template = simpleTAL.compileHTMLTemplate(file(filename))
self.cache[filename] = (template,mtime)
return template
def render(self, filename, d=None):
context = simpleTALES.Context()
if d:
for name, value in d.items():
context.addGlobal(name, value)
template = self.getTemplate(filename)
out = StringIO()
template.expand(context,out)
return out.getvalue()
# Create one
tr = TemplateRenderer()
Then your page would do something like (untested):
class Page:
def _q_index(self, request):
d = {'name': 'Matt', 'somethingElse': 'whatever'}
return tr.render('template.html', d)
There may be a (much) better way of doing all this (it's pretty much the
first time I've ever used either of Quixote and SimpleTAL) but I hope it
helps.
Cheers, Matt
--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: matt@pollenation.net