lib/template.py
author Tero Marttila <terom@fixme.fi>
Sun, 08 Feb 2009 02:29:23 +0200
branchsites
changeset 42 5a72c00c4ae4
parent 40 71ab68f31a1c
permissions -rw-r--r--
more fiddling around with the irclogs layout/css, add query args to URL
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Template handler
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
# use Mako
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
from mako import exceptions
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
from mako.template import Template
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
     8
import mako.lookup
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
# for http.ResponseError
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
import http
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
26
9d3beac1b196 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
    13
import helpers
9d3beac1b196 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
    14
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
# path to template files
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
TEMPLATE_DIR = "templates"
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
# path to cached templates
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
CACHE_DIR = "cache/templates"
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
# template file extension
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
TEMPLATE_EXT = "tmpl"
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
class TemplateError (http.ResponseError) :
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    """
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        Raised by the template module functions
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    """
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    pass
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    31
class TemplateLoader (mako.lookup.TemplateLookup) :
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    """
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    33
        Our own specialization of mako's TemplateLookup
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    """
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
42
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    36
    def __init__ (self, path, fileext=TEMPLATE_EXT, **env) :
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    37
        """
42
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    38
            Initialize to load templates located at path, with the given file extension.
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    39
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    40
            The given **env list is supplied to every template render
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    41
        """
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    43
        # store
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    44
        self.path = path
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    45
        self.fileext = fileext
42
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    46
        self.env = env
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    47
            
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    48
        # build the TemplateLookup
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    49
        super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR)
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    50
    
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    51
    @staticmethod
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    52
    def _render (tpl, env, params) :
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    53
        """
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    54
            Render the given template with given env/params, returning the output as a unicode string, or raising a TemplateError
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    55
        """
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    56
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    57
        # build the context from our superglobals, env, and params
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    58
        ctx = dict(
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    59
            h       = helpers,
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    60
        )
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    61
        ctx.update(env)
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    62
        ctx.update(params)
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    63
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    64
        try :
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    65
            return tpl.render_unicode(**ctx)
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    66
        
42
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    67
        # a template may render other templates
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    68
        except TemplateError :
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    69
            raise
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    70
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    71
        except :
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    72
            details = exceptions.text_error_template().render()
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    73
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    74
            raise TemplateError("Template render failed", status='500 Internal Server Error', details=details)
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    75
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    77
    def lookup (self, name) :
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    78
        """
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    79
            Looks up a template based on the bare "name", which does not include the path or file extension
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    80
        """
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    81
        
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    82
        try :
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    83
            return self.get_template("%s.%s" % (name, self.fileext))
8
0ce1f471e9d7 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    85
        except :
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    86
            raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    87
    
40
71ab68f31a1c the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    88
    def render (self, name, **params) :
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    89
        """
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    90
            Render a template, using lookup() on the given name
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    91
        """
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    92
42
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    93
        return self._render(self.lookup(name), self.env, params)
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    94
40
71ab68f31a1c the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    95
    def render_to_response (self, name, **params) :
71ab68f31a1c the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    96
        """
71ab68f31a1c the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    97
            Render a template, returning a http.Response object
71ab68f31a1c the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    98
        """
71ab68f31a1c the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    99
71ab68f31a1c the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
   100
        return http.Response(self.render(name, **params))
71ab68f31a1c the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
   101
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   102
    @classmethod
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   103
    def load (cls, path) :
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   104
        """
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   105
            Loads a template from a specific file
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   106
        """
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   107
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   108
        try :
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   109
            return Template(filename=path, module_directory=CACHE_DIR)
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   110
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   111
        except :
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   112
            raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   113
    
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   114
    @classmethod
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   115
    def render_file (cls, path, **params) :
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   116
        """
42
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   117
            Render a template, using load() on the given path. No global environment vars are defined for the render.
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   118
        """
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   119
42
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   120
        return cls._render(cls.load(path), dict(), params)
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   121
42
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   122
    @classmethod
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   123
    def render_template (cls, template, **params) :
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   124
        """
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   125
            Render the given template object. No global environment vars are defined for the render.
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   126
        """
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   127
        
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   128
        return cls._render(template, dict(), params)