terom@8: """ terom@8: Template handler terom@8: """ terom@8: terom@8: from mako import exceptions terom@8: from mako.template import Template terom@32: import mako.lookup terom@8: terom@46: from qmsk.web import http, helpers terom@26: terom@8: # path to template files terom@8: TEMPLATE_DIR = "templates" terom@8: terom@8: # path to cached templates terom@8: CACHE_DIR = "cache/templates" terom@8: terom@8: # template file extension terom@8: TEMPLATE_EXT = "tmpl" terom@8: terom@8: class TemplateError (http.ResponseError) : terom@8: """ terom@8: Raised by the template module functions terom@8: """ terom@8: terom@8: pass terom@8: terom@32: class TemplateLoader (mako.lookup.TemplateLookup) : terom@8: """ terom@32: Our own specialization of mako's TemplateLookup terom@8: """ terom@8: terom@42: def __init__ (self, path, fileext=TEMPLATE_EXT, **env) : terom@32: """ terom@42: Initialize to load templates located at path, with the given file extension. terom@42: terom@42: The given **env list is supplied to every template render terom@32: """ terom@8: terom@32: # store terom@32: self.path = path terom@32: self.fileext = fileext terom@42: self.env = env terom@42: terom@42: # build the TemplateLookup terom@42: super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR) terom@42: terom@42: @staticmethod terom@42: def _render (tpl, env, params) : terom@42: """ terom@42: Render the given template with given env/params, returning the output as a unicode string, or raising a TemplateError terom@42: """ terom@42: terom@42: # build the context from our superglobals, env, and params terom@42: ctx = dict( terom@42: h = helpers, terom@42: ) terom@42: ctx.update(env) terom@42: ctx.update(params) terom@42: terom@42: try : terom@42: return tpl.render_unicode(**ctx) terom@32: terom@42: # a template may render other templates terom@42: except TemplateError : terom@42: raise terom@42: terom@42: except : terom@42: details = exceptions.text_error_template().render() terom@42: terom@42: raise TemplateError("Template render failed", status='500 Internal Server Error', details=details) terom@42: terom@8: terom@32: def lookup (self, name) : terom@32: """ terom@32: Looks up a template based on the bare "name", which does not include the path or file extension terom@32: """ terom@32: terom@32: try : terom@32: return self.get_template("%s.%s" % (name, self.fileext)) terom@8: terom@32: except : terom@32: raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render()) terom@32: terom@40: def render (self, name, **params) : terom@32: """ terom@32: Render a template, using lookup() on the given name terom@32: """ terom@32: terom@42: return self._render(self.lookup(name), self.env, params) terom@32: terom@40: def render_to_response (self, name, **params) : terom@40: """ terom@40: Render a template, returning a http.Response object terom@40: """ terom@40: terom@40: return http.Response(self.render(name, **params)) terom@40: terom@32: @classmethod terom@32: def load (cls, path) : terom@32: """ terom@32: Loads a template from a specific file terom@32: """ terom@32: terom@32: try : terom@32: return Template(filename=path, module_directory=CACHE_DIR) terom@32: terom@32: except : terom@32: raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render()) terom@32: terom@32: @classmethod terom@32: def render_file (cls, path, **params) : terom@32: """ terom@42: Render a template, using load() on the given path. No global environment vars are defined for the render. terom@32: """ terom@32: terom@42: return cls._render(cls.load(path), dict(), params) terom@32: terom@42: @classmethod terom@42: def render_template (cls, template, **params) : terom@42: """ terom@42: Render the given template object. No global environment vars are defined for the render. terom@42: """ terom@42: terom@42: return cls._render(template, dict(), params)