Template.render understands jinja2 contexts now

This commit is contained in:
Vladislav Polukhin 2015-01-19 15:03:54 +07:00
parent 7b5d514238
commit 6cde4de534

View File

@ -4,6 +4,7 @@ from django.template import (
import_library, import_library,
) )
from jinja2 import Template as _Jinja2Template from jinja2 import Template as _Jinja2Template
from jinja2.runtime import Context as _Jinja2Context
# Merge with ``django.template``. # Merge with ``django.template``.
from django.template import __all__ from django.template import __all__
@ -45,14 +46,17 @@ class Template(_Jinja2Template):
here between implementing Django's interface while still supporting here between implementing Django's interface while still supporting
Jinja's own call syntax as well. Jinja's own call syntax as well.
""" """
if context is None: dict_ = {}
context = {}
else: if isinstance(context, DjangoContext):
context = dict_from_django_context(context) dict_ = dict_from_django_context(context)
assert isinstance(context, dict) # Required for **-operator.
if isinstance(context, _Jinja2Context):
dict_ = context.get_all()
# It'd be nice to move this only into the test env # It'd be nice to move this only into the test env
signals.template_rendered.send(sender=self, template=self, context=Context(context)) signals.template_rendered.send(sender=self, template=self, context=DjangoContext(dict_))
return super(Template, self).render(**context) return super(Template, self).render(**dict_)
@property @property
def origin(self): def origin(self):
@ -62,18 +66,15 @@ class Template(_Jinja2Template):
def dict_from_django_context(context): def dict_from_django_context(context):
"""Flattens a Django :class:`django.template.context.Context` object. """Flattens a Django :class:`django.template.context.Context` object.
""" """
if not isinstance(context, DjangoContext): dict_ = {}
return context # Jinja2 internally converts the context instance to a dictionary, thus
else: # we need to store the current_app attribute as a key/value pair.
dict_ = {} dict_['_current_app'] = getattr(context, 'current_app', None)
# Jinja2 internally converts the context instance to a dictionary, thus
# we need to store the current_app attribute as a key/value pair.
dict_['_current_app'] = getattr(context, 'current_app', None)
# Newest dicts are up front, so update from oldest to newest. # Newest dicts are up front, so update from oldest to newest.
for subcontext in reversed(list(context)): for subcontext in reversed(list(context)):
dict_.update(dict_from_django_context(subcontext)) dict_.update(subcontext)
return dict_ return dict_
# libraries to load by default for a new environment # libraries to load by default for a new environment