On 31/08/07, Ryan Tracey <[EMAIL PROTECTED]> wrote:
> On 31/08/2007, Anand <[EMAIL PROTECTED]> wrote:
> >
> > > from mako.lookup import TemplateLookup
> > > mylookup = TemplateLookup(directories=['templates/'],
> > > module_directory='mako_modules')
> > >
> > > def serve_template(templatename, **kwargs):
> > > mytemplate = mylookup.get_template(templatename)
> > > print mytemplate.render(**kwargs)
> >
> >
> > I think templator approach is the best.
>
> I have no preferences as yet. Just in the process of converting a
> web.py 0.1 app into a web.py 0.2 app and hit some snags with templator
> -- probably just my own conditioning that needs to change ;-)
>
> Do you reckon there'll be a performance hit associated with using a
> third-party templating system?
>
> > from mako.lookup import TemplateLookup
> >
> > class mako_render:
> > def __init__(path):
> > self._lookup = TemplateLookup(directories=[path],
> > module_directory='mako_modules')
> >
> > def __getattr__(self, name):
> > path = name + '.html'
> > t = self._lookup.get_template(path)
> > t.__call__ = t.render
> > return t
> >
> > render = mako_render('templates/')
> >
> > class hello:
> > def GET(self, name):
> > i = web.input(times=1)
> > if not name:
> > name = 'world'
> > print render.hello(name=name, times=int(i.times))
>
> Thanks very much Anand, I'll give this a try.
Hi Anand
I tried out the method you suggested but had to make a few changes get
it to work (in my environment?) For instance, despite the "t.__call__
= t.render" in __getattr__ I cannot call render.hello(...). I can, as
expected, call render.hello.__call__(...). I got around this by having
__getattr__ return t.render instead of just t. Also, to get around the
problem of web.py wrapping the template's output in <pre> tags I call
web.header(...) before I call render.hello(...). Here is what I am
doing:
from mako.lookup import TemplateLookup
class mako_render:
def __init__(self, path):
self._lookup = TemplateLookup(directories=[path],
module_directory='mako_modules')
def __getattr__(self, name):
path = name + '.html'
t = self._lookup.get_template(path)
#t.__call__ = t.render
return t.render
render = mako_render('templates/')
class hello:
def GET(self, name):
i = web.input(times=1)
if not name:
name = 'world'
web.header("Content-Type", "text/html; charset=utf-8")
print render.hello(name=name, times=int(i.times))
Can you see any way I could get around having to use the web.header
method? By returning the method (t.render) and not the object (t) am I
necessitating the use of web.header? I dunno, my OO knowledge is very
poor, for instance I was blown away by __gettr__ and had to contrive a
simple case to understand it. Don't laugh:
In [7]: class Foo:
...: def __init__(self, action):
...: self.action = action
...: def __getattr__(self, thing):
...: return thing +' '+ self.action
...:
In [8]: f = Foo('barks')
In [9]: f.dog
Out[9]: 'dog barks'
Thanks again for the suggestion -- I now know a bit more than I did on Friday!
Cheers,
Ryan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---