Send 'template_rendered' signal from Jinja2.

Send the signal only under DEBUG=True just like Django.
This commit is contained in:
Umair Khan 2016-05-23 18:44:07 +05:00
parent 29859c191d
commit daf3d51d4b
2 changed files with 11 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import sys
import jinja2 import jinja2
from django.utils import six from django.utils import six
from django.test.signals import template_rendered
from django.template.backends import jinja2 as django_jinja2 from django.template.backends import jinja2 as django_jinja2
from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
from django.utils.module_loading import import_string from django.utils.module_loading import import_string
@ -22,12 +23,14 @@ class Jinja2(django_jinja2.Jinja2):
# We need to remove `context_processors` from `OPTIONS` because # We need to remove `context_processors` from `OPTIONS` because
# `Environment` doesn't expect it # `Environment` doesn't expect it
self.context_processors = params['OPTIONS'].pop('context_processors', []) self.context_processors = params['OPTIONS'].pop('context_processors', [])
self.debug = params['OPTIONS'].pop('debug', False)
super(Jinja2, self).__init__(params, *args, **kwargs) super(Jinja2, self).__init__(params, *args, **kwargs)
def get_template(self, template_name): def get_template(self, template_name):
try: try:
return Template(self.env.get_template(template_name), return Template(self.env.get_template(template_name),
self.context_processors) self.context_processors,
self.debug)
except jinja2.TemplateNotFound as exc: except jinja2.TemplateNotFound as exc:
six.reraise(TemplateDoesNotExist, TemplateDoesNotExist(exc.args), six.reraise(TemplateDoesNotExist, TemplateDoesNotExist(exc.args),
sys.exc_info()[2]) sys.exc_info()[2])
@ -43,8 +46,9 @@ class Template(django_jinja2.Template):
processors to the context before passing it to the `render` processors to the context before passing it to the `render`
function. function.
""" """
def __init__(self, template, context_processors, *args, **kwargs): def __init__(self, template, context_processors, debug, *args, **kwargs):
self.context_processors = context_processors self.context_processors = context_processors
self.debug = debug
super(Template, self).__init__(template, *args, **kwargs) super(Template, self).__init__(template, *args, **kwargs)
def render(self, context=None, request=None): def render(self, context=None, request=None):
@ -63,4 +67,8 @@ class Template(django_jinja2.Template):
cp = import_string(context_processor) cp = import_string(context_processor)
context.update(cp(request)) context.update(cp(request))
if self.debug:
template_rendered.send(sender=self, template=self,
context=context)
return self.template.render(context) return self.template.render(context)

View File

@ -251,6 +251,7 @@ TEMPLATES = [
], ],
'APP_DIRS': False, 'APP_DIRS': False,
'OPTIONS': { 'OPTIONS': {
'debug': DEBUG,
'environment': 'zproject.jinja2.environment', 'environment': 'zproject.jinja2.environment',
'extensions': [ 'extensions': [
'jinja2.ext.i18n', 'jinja2.ext.i18n',