|
1 from mako import exceptions |
|
2 from mako.lookup import TemplateLookup |
|
3 |
|
4 import settings, helpers |
|
5 |
|
6 import log |
|
7 |
|
8 _lookup = TemplateLookup( |
|
9 directories=[settings.TEMPLATE_DIR], |
|
10 module_directory='%s/cache' % settings.TEMPLATE_DIR, |
|
11 output_encoding='utf-8', |
|
12 filesystem_checks=False, # this may need to be changed if used in a long-term process |
|
13 ) |
|
14 |
|
15 TEMPLATE_GLOBALS = dict( |
|
16 h = helpers, |
|
17 version = settings.VERSION, |
|
18 ) |
|
19 |
|
20 class Template (object) : |
|
21 def __init__ (self, name) : |
|
22 self.name = name |
|
23 self.tpl = _lookup.get_template("%s.%s" % (name, settings.TEMPLATE_EXT)) |
|
24 |
|
25 def render (self, **data) : |
|
26 data.update(TEMPLATE_GLOBALS) |
|
27 |
|
28 try : |
|
29 log.template.debug("render %s with %s", self.name, data) |
|
30 return self.tpl.render(**data) |
|
31 except : |
|
32 data = exceptions.text_error_template().render() |
|
33 log.template.error(data) |
|
34 |
|
35 raise |
|
36 |
|
37 def render_to (self, file, **data) : |
|
38 fh = open(file, "w") |
|
39 fh.write(self.render(**data)) |
|
40 fh.close() |
|
41 |
|
42 # templates |
|
43 gallery = Template("gallery") |
|
44 image = Template("image") |
|
45 |