lib/template.py
author Tero Marttila <terom@fixme.fi>
Sat, 07 Feb 2009 21:21:10 +0200
branchsites
changeset 181 54d0edb81cb8
parent 173 f1d36d7b361e
child 42 5a72c00c4ae4
permissions -rw-r--r--
the handlers work now
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Template handler
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
# use Mako
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
from mako import exceptions
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
from mako.template import Template
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
     8
import mako.lookup
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
# for http.ResponseError
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
import http
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
167
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    13
import helpers
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    14
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
# path to template files
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
TEMPLATE_DIR = "templates"
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
# path to cached templates
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
CACHE_DIR = "cache/templates"
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
# template file extension
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
TEMPLATE_EXT = "tmpl"
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
class TemplateError (http.ResponseError) :
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        Raised by the template module functions
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    pass
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    31
def render (tpl, **params) :
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    """
163
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 153
diff changeset
    33
        Render the given template, returning the output as a unicode string, or raising a TemplateError
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    try :
167
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    37
        return tpl.render_unicode(
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    38
            # global helper stuff
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    39
            h           = helpers,
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    40
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    41
            # render-specific params
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    42
            **params
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    43
        )
153
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    44
    
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    45
    # a template may render other templates
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    46
    except TemplateError :
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    47
        raise
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    except :
153
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    50
        details = exceptions.text_error_template().render()
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    51
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    52
        raise TemplateError("Template render failed", status='500 Internal Server Error', details=details)
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    54
class TemplateLoader (mako.lookup.TemplateLookup) :
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    """
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    56
        Our own specialization of mako's TemplateLookup
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    59
    def __init__ (self, path, fileext=TEMPLATE_EXT) :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    60
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    61
            Initialize to load templates located at path, with the given file extension
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    62
        """
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    64
        # store
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    65
        self.path = path
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    66
        self.fileext = fileext
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    67
        
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    68
        # XXX: separate cache?
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    69
        super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR)
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    71
    def lookup (self, name) :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    72
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    73
            Looks up a template based on the bare "name", which does not include the path or file extension
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    74
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    75
        
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    76
        try :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    77
            return self.get_template("%s.%s" % (name, self.fileext))
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    79
        except :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    80
            raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    81
    
181
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    82
    def render (self, name, **params) :
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    83
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    84
            Render a template, using lookup() on the given name
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    85
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    86
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    87
        return render(self.lookup(name), **params)
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    88
181
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    89
    def render_to_response (self, name, **params) :
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    90
        """
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    91
            Render a template, returning a http.Response object
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    92
        """
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    93
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    94
        return http.Response(self.render(name, **params))
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    95
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    96
    @classmethod
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    97
    def load (cls, path) :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    98
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    99
            Loads a template from a specific file
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   100
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   101
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   102
        try :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   103
            return Template(filename=path, module_directory=CACHE_DIR)
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   104
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   105
        except :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   106
            raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   107
    
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   108
    @classmethod
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   109
    def render_file (cls, path, **params) :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   110
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   111
            Render a template, using load() on the given path
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   112
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   113
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   114
        return render(cls.load(path), **params)
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   115
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   116