template.py
changeset 71 0162c2b21dc5
parent 70 add1c1f7831c
equal deleted inserted replaced
70:add1c1f7831c 71:0162c2b21dc5
    23         Raised by the template module functions
    23         Raised by the template module functions
    24     """
    24     """
    25 
    25 
    26     pass
    26     pass
    27 
    27 
       
    28 def fmt_mako_exception () :
       
    29     """
       
    30         Returns a string containing the Mako-ized error traceback (so that it includes the template data)
       
    31     """
       
    32 
       
    33     return exceptions.text_error_template().render()
       
    34  
    28 class TemplateLoader (mako.lookup.TemplateLookup) :
    35 class TemplateLoader (mako.lookup.TemplateLookup) :
    29     """
    36     """
    30         Our own specialization of mako's TemplateLookup
    37         Our own specialization of mako's TemplateLookup
    31     """
    38     """
    32 
    39 
    80         except TemplateError :
    87         except TemplateError :
    81             raise
    88             raise
    82 
    89 
    83         except :
    90         except :
    84             # raise a TemplateError with the details
    91             # raise a TemplateError with the details
    85             details = exceptions.text_error_template().render()
    92             raise TemplateError("tpl=%r, params=%r: %s" % (tpl, params, fmt_mako_exception()))
    86 
       
    87             raise TemplateError("Template render failed", status='500 Internal Server Error', details=details)
       
    88         
    93         
    89         else :
    94         else :
    90             # return the unicode data
    95             # return the unicode data
    91             return buf.getvalue()
    96             return buf.getvalue()
    92 
    97 
    97         
   102         
    98         try :
   103         try :
    99             return self.get_template("%s.%s" % (name, self.fileext))
   104             return self.get_template("%s.%s" % (name, self.fileext))
   100 
   105 
   101         except :
   106         except :
   102             raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
   107             raise TemplateError("name=%r: %s" % (name, fmt_mako_exception()))
   103     
   108     
   104     def render (self, name, **params) :
   109     def render (self, name, **params) :
   105         """
   110         """
   106             Render a template, using lookup() on the given name
   111             Render a template, using lookup() on the given name
   107         """
   112         """
   122 
   127 
   123         try :
   128         try :
   124             return Template(filename=path, module_directory=CACHE_DIR)
   129             return Template(filename=path, module_directory=CACHE_DIR)
   125 
   130 
   126         except :
   131         except :
   127             raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
   132             raise TemplateError("path=%r: %s" % (path, fmt_mako_exception()))
   128     
   133     
   129     def render_file (self, path, **params) :
   134     def render_file (self, path, **params) :
   130         """
   135         """
   131             Render a template, using load() on the given path. No global environment vars are defined for the render.
   136             Render a template, using load() on the given path. No global environment vars are defined for the render.
   132         """
   137         """