terom@8: """ terom@8: Template handler terom@8: """ terom@8: terom@8: # use Mako terom@8: from mako import exceptions terom@8: from mako.template import Template terom@32: import mako.lookup terom@8: terom@8: # for http.ResponseError terom@8: import http terom@8: terom@26: import 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: def render (tpl, **params) : terom@8: """ terom@21: Render the given template, returning the output as a unicode string, or raising a TemplateError terom@8: """ terom@8: terom@8: try : terom@26: return tpl.render_unicode( terom@26: # global helper stuff terom@26: h = helpers, terom@26: terom@26: # render-specific params terom@26: **params terom@26: ) terom@11: terom@11: # a template may render other templates terom@11: except TemplateError : terom@11: raise terom@8: terom@8: except : terom@11: details = exceptions.text_error_template().render() terom@11: terom@11: raise TemplateError("Template render failed", status='500 Internal Server Error', details=details) 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@32: def __init__ (self, path, fileext=TEMPLATE_EXT) : terom@32: """ terom@32: Initialize to load templates located at path, with the given file extension terom@32: """ terom@8: terom@32: # store terom@32: self.path = path terom@32: self.fileext = fileext terom@32: terom@32: # XXX: separate cache? terom@32: super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR) 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@32: return render(self.lookup(name), **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@32: Render a template, using load() on the given path terom@32: """ terom@32: terom@32: return render(cls.load(path), **params) terom@32: terom@32: