This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
python-emails/emails/template/jinja_template.py

29 lines
878 B
Python

# encoding: utf-8
from __future__ import unicode_literals
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):
return self.environment.from_string(self.template_text)
def render(self, **kwargs):
return self.template.render(**kwargs)