Mercurial > hg > genshi_view
changeset 21:807c8eef8098
add a handler for tempita; should rename the package (at some point) template_view
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 17 Nov 2010 11:02:33 -0800 |
parents | cf19d2e129de |
children | f088f0b54b55 |
files | genshi_view/template/+package+/handlers.py genshi_view/template/setup.py_tmpl |
diffstat | 2 files changed, 32 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/genshi_view/template/+package+/handlers.py Wed May 05 11:59:39 2010 -0700 +++ b/genshi_view/template/+package+/handlers.py Wed Nov 17 11:02:33 2010 -0800 @@ -3,8 +3,11 @@ these are instantiated for every request, then called """ +import os +from pkg_resources import resource_filename from urlparse import urlparse from webob import Response, exc +from tempita import HTMLTemplate class HandlerMatchException(Exception): """the handler doesn't match the request""" @@ -49,6 +52,31 @@ def redirect(self, location): raise exc.HTTPSeeOther(location=location) +class TempitaHandler(Handler): + + template_dirs = [ resource_filename(__name__, 'templates') ] + + def __init__(self, app, request): + Handler.__init__(self, app, request) + self.data = { 'request': request, + 'link': self.link } + + def __call__(self): + return getattr(self, self.request.method.title())() + + def find_template(self, template): + for d in self.template_dirs: + path = os.path.join(d, template) + if os.path.exists(path): + return HTMLTemplate.from_filename(path) + + def Get(self): + # needs to have self.template set + template = self.find_template(self.template) + return Response(content_type='text/html', + body=template.substitute(**self.data)) + + class GenshiHandler(Handler): def __init__(self, app, request): @@ -65,6 +93,8 @@ return Response(content_type='text/html', body=template.generate(**self.data).render('html')) + + class Index(GenshiHandler): template = 'index.html' methods=set(['GET', 'POST'])