summaryrefslogtreecommitdiff
path: root/renderer.py
blob: ba08e36d5e03ca23b2a08f077dbbe511f6881a41 (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
41
import os
import web
import mako
from mako.lookup import TemplateLookup

import helper
import highlighting

class Renderer:
    """
    Renderer loading the correct Mako Templates
    """
    def __init__ (self):
        self.lookup = TemplateLookup(
                directories=[helper.appdir('templates')],
                module_directory = "/tmp/webpy/portato/",
                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()