summaryrefslogtreecommitdiff
path: root/renderer.py
blob: a24aadd08ac4fb0340f8e0244d5c41fc3ccd0017 (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
38
39
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):
        web.header("Content-Type", "text/html;charset=utf-8")
        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, **kwargs)

    __call__ = render

    def get_tpl (self, tpl, level = ""):
        if not tpl.endswith(".mako"):
            tpl = tpl+".mako"
        
        tpl = os.path.join(level, tpl)

        return self.lookup.get_template(tpl)
    
# the one and only instance :)
render = Renderer()