From f0012811f8af8b1334b46781861a6dd3777ee392 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Mon, 3 May 2010 18:21:53 +0200 Subject: First import from portato proj --- renderer.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 renderer.py (limited to 'renderer.py') diff --git a/renderer.py b/renderer.py new file mode 100644 index 0000000..b8ce716 --- /dev/null +++ b/renderer.py @@ -0,0 +1,40 @@ +import os +import web +import mako +from mako.lookup import TemplateLookup + +import helper + +class Renderer: + """ + Renderer loading the correct Mako Templates + """ + def __init__ (self): + self.lookup = TemplateLookup( + directories=[helper.appdir('templates')], + module_directory = "/tmp/webpy/kosten/", + input_encoding='utf-8', + output_encoding='utf-8', + format_exceptions = True) + + def render (self, tpl, level = "pages" , **kwargs): + try: + t = self.get_tpl(tpl, level) + except mako.exceptions.TopLevelLookupException, e: + raise web.ctx.app_stack[-1].notfound(tpl) + + return t.render(h = helper, url = helper.url, w = web, _hl = highlighting, **kwargs) + + __call__ = render + + def get_tpl (self, tpl, level): + return self.lookup.get_template(self.get_tpl_name(tpl, level)) + + def get_tpl_name (self, tpl, level): + if not tpl.endswith(".mako"): + tpl = tpl+".mako" + + return os.path.join(level, tpl) + +# the one and only instance :) +render = Renderer() -- cgit v1.2.3-70-g09d2