lib/template.py
branchsites
changeset 32 be954df4f0e8
parent 26 9d3beac1b196
child 40 71ab68f31a1c
equal deleted inserted replaced
31:107062ebb6f9 32:be954df4f0e8
     3 """
     3 """
     4 
     4 
     5 # use Mako
     5 # use Mako
     6 from mako import exceptions
     6 from mako import exceptions
     7 from mako.template import Template
     7 from mako.template import Template
     8 from mako.lookup import TemplateLookup
     8 import mako.lookup
     9 
     9 
    10 # for http.ResponseError
    10 # for http.ResponseError
    11 import http
    11 import http
    12 
    12 
    13 import helpers
    13 import helpers
    19 CACHE_DIR = "cache/templates"
    19 CACHE_DIR = "cache/templates"
    20 
    20 
    21 # template file extension
    21 # template file extension
    22 TEMPLATE_EXT = "tmpl"
    22 TEMPLATE_EXT = "tmpl"
    23 
    23 
    24 
       
    25 # our Mako template lookup handler
       
    26 _lookup = TemplateLookup(directories=[TEMPLATE_DIR], module_directory=CACHE_DIR)
       
    27 
       
    28 
       
    29 class TemplateError (http.ResponseError) :
    24 class TemplateError (http.ResponseError) :
    30     """
    25     """
    31         Raised by the template module functions
    26         Raised by the template module functions
    32     """
    27     """
    33 
    28 
    34     pass
    29     pass
    35 
    30 
    36 def lookup (name) :
    31 def render (tpl, **params) :
    37     """
       
    38         Looks up a template based on the bare "name", which does not include the path or file extension
       
    39     """
       
    40     
       
    41     try :
       
    42         return _lookup.get_template("%s.%s" % (name, TEMPLATE_EXT))
       
    43 
       
    44     except :
       
    45         raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
       
    46 
       
    47 def load (path) :
       
    48     """
       
    49         Loads a template from a specific file
       
    50     """
       
    51 
       
    52     try :
       
    53         return Template(filename=path, module_directory=CACHE_DIR)
       
    54 
       
    55     except :
       
    56         raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
       
    57 
       
    58 def render_template (tpl, **params) :
       
    59     """
    32     """
    60         Render the given template, returning the output as a unicode string, or raising a TemplateError
    33         Render the given template, returning the output as a unicode string, or raising a TemplateError
    61     """
    34     """
    62 
    35 
    63     try :
    36     try :
    76     except :
    49     except :
    77         details = exceptions.text_error_template().render()
    50         details = exceptions.text_error_template().render()
    78 
    51 
    79         raise TemplateError("Template render failed", status='500 Internal Server Error', details=details)
    52         raise TemplateError("Template render failed", status='500 Internal Server Error', details=details)
    80 
    53 
    81 def render (name, **params) :
    54 class TemplateLoader (mako.lookup.TemplateLookup) :
    82     """
    55     """
    83         Render a template, using lookup() on the given name
    56         Our own specialization of mako's TemplateLookup
    84     """
    57     """
    85 
    58 
    86     return render_template(lookup(name), **params)
    59     def __init__ (self, path, fileext=TEMPLATE_EXT) :
       
    60         """
       
    61             Initialize to load templates located at path, with the given file extension
       
    62         """
    87 
    63 
    88 def render_file (path, **params) :
    64         # store
    89     """
    65         self.path = path
    90         Render a template, using load() on the given path
    66         self.fileext = fileext
    91     """
    67         
       
    68         # XXX: separate cache?
       
    69         super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR)
    92 
    70 
    93     return render_template(load(path), **params)
    71     def lookup (self, name) :
       
    72         """
       
    73             Looks up a template based on the bare "name", which does not include the path or file extension
       
    74         """
       
    75         
       
    76         try :
       
    77             return self.get_template("%s.%s" % (name, self.fileext))
    94 
    78 
       
    79         except :
       
    80             raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
       
    81     
       
    82     def render (name, **params) :
       
    83         """
       
    84             Render a template, using lookup() on the given name
       
    85         """
       
    86 
       
    87         return render(self.lookup(name), **params)
       
    88 
       
    89     @classmethod
       
    90     def load (cls, path) :
       
    91         """
       
    92             Loads a template from a specific file
       
    93         """
       
    94 
       
    95         try :
       
    96             return Template(filename=path, module_directory=CACHE_DIR)
       
    97 
       
    98         except :
       
    99             raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
       
   100     
       
   101     @classmethod
       
   102     def render_file (cls, path, **params) :
       
   103         """
       
   104             Render a template, using load() on the given path
       
   105         """
       
   106 
       
   107         return render(cls.load(path), **params)
       
   108 
       
   109