diff -r 9585441a4bfb -r 5a72c00c4ae4 lib/template.py --- a/lib/template.py Sun Feb 08 00:29:36 2009 +0200 +++ b/lib/template.py Sun Feb 08 02:29:23 2009 +0200 @@ -28,45 +28,51 @@ pass -def render (tpl, **params) : - """ - Render the given template, returning the output as a unicode string, or raising a TemplateError - """ - - try : - return tpl.render_unicode( - # global helper stuff - h = helpers, - - # render-specific params - **params - ) - - # a template may render other templates - except TemplateError : - raise - - except : - details = exceptions.text_error_template().render() - - raise TemplateError("Template render failed", status='500 Internal Server Error', details=details) - class TemplateLoader (mako.lookup.TemplateLookup) : """ Our own specialization of mako's TemplateLookup """ - def __init__ (self, path, fileext=TEMPLATE_EXT) : + def __init__ (self, path, fileext=TEMPLATE_EXT, **env) : """ - Initialize to load templates located at path, with the given file extension + Initialize to load templates located at path, with the given file extension. + + The given **env list is supplied to every template render """ # store self.path = path self.fileext = fileext + self.env = env + + # build the TemplateLookup + super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR) + + @staticmethod + def _render (tpl, env, params) : + """ + Render the given template with given env/params, returning the output as a unicode string, or raising a TemplateError + """ + + # build the context from our superglobals, env, and params + ctx = dict( + h = helpers, + ) + ctx.update(env) + ctx.update(params) + + try : + return tpl.render_unicode(**ctx) - # XXX: separate cache? - super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR) + # a template may render other templates + except TemplateError : + raise + + except : + details = exceptions.text_error_template().render() + + raise TemplateError("Template render failed", status='500 Internal Server Error', details=details) + def lookup (self, name) : """ @@ -84,7 +90,7 @@ Render a template, using lookup() on the given name """ - return render(self.lookup(name), **params) + return self._render(self.lookup(name), self.env, params) def render_to_response (self, name, **params) : """ @@ -108,9 +114,15 @@ @classmethod def render_file (cls, path, **params) : """ - Render a template, using load() on the given path + Render a template, using load() on the given path. No global environment vars are defined for the render. """ - return render(cls.load(path), **params) + return cls._render(cls.load(path), dict(), params) - + @classmethod + def render_template (cls, template, **params) : + """ + Render the given template object. No global environment vars are defined for the render. + """ + + return cls._render(template, dict(), params)