26 Raised by the template module functions |
26 Raised by the template module functions |
27 """ |
27 """ |
28 |
28 |
29 pass |
29 pass |
30 |
30 |
31 def render (tpl, **params) : |
|
32 """ |
|
33 Render the given template, returning the output as a unicode string, or raising a TemplateError |
|
34 """ |
|
35 |
|
36 try : |
|
37 return tpl.render_unicode( |
|
38 # global helper stuff |
|
39 h = helpers, |
|
40 |
|
41 # render-specific params |
|
42 **params |
|
43 ) |
|
44 |
|
45 # a template may render other templates |
|
46 except TemplateError : |
|
47 raise |
|
48 |
|
49 except : |
|
50 details = exceptions.text_error_template().render() |
|
51 |
|
52 raise TemplateError("Template render failed", status='500 Internal Server Error', details=details) |
|
53 |
|
54 class TemplateLoader (mako.lookup.TemplateLookup) : |
31 class TemplateLoader (mako.lookup.TemplateLookup) : |
55 """ |
32 """ |
56 Our own specialization of mako's TemplateLookup |
33 Our own specialization of mako's TemplateLookup |
57 """ |
34 """ |
58 |
35 |
59 def __init__ (self, path, fileext=TEMPLATE_EXT) : |
36 def __init__ (self, path, fileext=TEMPLATE_EXT, **env) : |
60 """ |
37 """ |
61 Initialize to load templates located at path, with the given file extension |
38 Initialize to load templates located at path, with the given file extension. |
|
39 |
|
40 The given **env list is supplied to every template render |
62 """ |
41 """ |
63 |
42 |
64 # store |
43 # store |
65 self.path = path |
44 self.path = path |
66 self.fileext = fileext |
45 self.fileext = fileext |
|
46 self.env = env |
|
47 |
|
48 # build the TemplateLookup |
|
49 super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR) |
|
50 |
|
51 @staticmethod |
|
52 def _render (tpl, env, params) : |
|
53 """ |
|
54 Render the given template with given env/params, returning the output as a unicode string, or raising a TemplateError |
|
55 """ |
|
56 |
|
57 # build the context from our superglobals, env, and params |
|
58 ctx = dict( |
|
59 h = helpers, |
|
60 ) |
|
61 ctx.update(env) |
|
62 ctx.update(params) |
|
63 |
|
64 try : |
|
65 return tpl.render_unicode(**ctx) |
67 |
66 |
68 # XXX: separate cache? |
67 # a template may render other templates |
69 super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR) |
68 except TemplateError : |
|
69 raise |
|
70 |
|
71 except : |
|
72 details = exceptions.text_error_template().render() |
|
73 |
|
74 raise TemplateError("Template render failed", status='500 Internal Server Error', details=details) |
|
75 |
70 |
76 |
71 def lookup (self, name) : |
77 def lookup (self, name) : |
72 """ |
78 """ |
73 Looks up a template based on the bare "name", which does not include the path or file extension |
79 Looks up a template based on the bare "name", which does not include the path or file extension |
74 """ |
80 """ |
82 def render (self, name, **params) : |
88 def render (self, name, **params) : |
83 """ |
89 """ |
84 Render a template, using lookup() on the given name |
90 Render a template, using lookup() on the given name |
85 """ |
91 """ |
86 |
92 |
87 return render(self.lookup(name), **params) |
93 return self._render(self.lookup(name), self.env, params) |
88 |
94 |
89 def render_to_response (self, name, **params) : |
95 def render_to_response (self, name, **params) : |
90 """ |
96 """ |
91 Render a template, returning a http.Response object |
97 Render a template, returning a http.Response object |
92 """ |
98 """ |
106 raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render()) |
112 raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render()) |
107 |
113 |
108 @classmethod |
114 @classmethod |
109 def render_file (cls, path, **params) : |
115 def render_file (cls, path, **params) : |
110 """ |
116 """ |
111 Render a template, using load() on the given path |
117 Render a template, using load() on the given path. No global environment vars are defined for the render. |
112 """ |
118 """ |
113 |
119 |
114 return render(cls.load(path), **params) |
120 return cls._render(cls.load(path), dict(), params) |
115 |
121 |
116 |
122 @classmethod |
|
123 def render_template (cls, template, **params) : |
|
124 """ |
|
125 Render the given template object. No global environment vars are defined for the render. |
|
126 """ |
|
127 |
|
128 return cls._render(template, dict(), params) |