Removed obsolete comment.
[toast/alpenzoo.git] / django_template_backend_genshi / __init__.py
1 from django.template.backends.utils import csrf_input_lazy, csrf_token_lazy
2 import django.template.backends.base
3 import genshi.template
4
5
6 class GenshiEngine(django.template.backends.base.BaseEngine):
7     def __init__(self, params):
8         params = params.copy()
9         options = params.pop('OPTIONS').copy()
10         super(GenshiEngine, self).__init__(params)
11         loader = genshi.template.TemplateLoader(self.template_dirs, auto_reload=options['auto_reload'])
12         self.loader = loader
13
14
15     def from_string(self, template_code):
16         try:
17             template = genshi.template.MarkupTemplate(template_code)
18             return GenshiTemplateWrapper(template)
19         except genshi.TemplateCompilationFailed as exc:
20             raise django.template.TemplateSyntaxError(exc.args)
21
22
23     def get_template(self, template_name):
24         try:
25             template = self.loader.load(template_name)
26             return GenshiTemplateWrapper(self.loader.get_template(template_name))
27         except genshi.TemplateNotFound as exc:
28             raise django.template.TemplateDoesNotExist(exc.args)
29         except genshi.TemplateCompilationFailed as exc:
30             raise django.template.TemplateSyntaxError(exc.args)
31
32
33 class GenshiTemplateWrapper(object):
34     def __init__(self, template):
35         self.template = template
36
37     def render(self, context=None, request=None):
38         if context is None:
39             context = {}
40         if request is not None:
41             context['request'] = request
42             context['csrf_input'] = csrf_input_lazy(request)
43             context['csrf_token'] = csrf_token_lazy(request)
44         stream = self.template.generate(dict=context)
45         return stream.render()