summaryrefslogtreecommitdiff
path: root/renderer.py
blob: 6f983a4e06fa1e2938eaf919d602df9a9b8ac362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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, **kwargs):
        try:
            t = self.get_tpl(tpl)
        except mako.exceptions.TopLevelLookupException, e:
            raise web.ctx.app_stack[-1].notfound(tpl)
       
        return t.render(h = helper, url = helper.url, w = web, **kwargs)

    __call__ = render

    def get_tpl (self, tpl):
        if not tpl.endswith(".mako"):
            tpl = tpl+".mako"
        
        return self.lookup.get_template(tpl)
    
# the one and only instance :)
render = Renderer()