JinjaTemplate mini refactoring

This commit is contained in:
Sergey Lavrinenko 2015-04-02 00:35:09 +03:00
parent f457e7e889
commit 489d9bb8c9
1 changed files with 14 additions and 4 deletions

View File

@ -4,15 +4,25 @@ from .base import BaseTemplate
class JinjaTemplate(BaseTemplate):
"""
This template is mostly for demo purposes.
You probably want to subclass from it
and make more clear environment initialization.
"""
DEFAULT_JINJA_ENVIRONMENT = {}
def __init__(self, template_text, environment=None):
super(JinjaTemplate, self).__init__(template_text)
if environment:
self.environment = environment
else:
if 'jinja2' not in globals():
globals()['jinja2'] = __import__('jinja2')
self.environment = jinja2.Environment(**self.DEFAULT_JINJA_ENVIRONMENT)
def compile_template(self):
if 'jinja2' not in globals():
globals()['jinja2'] = __import__('jinja2')
self.environment = self.kwargs.get('environment', None) or jinja2.Environment(**self.DEFAULT_JINJA_ENVIRONMENT)
return self.environment.from_string(self.template_text)
def render(self, **kwargs):
return self.template.render(**kwargs)