From 5b1bb40216ff452e34198345fdad63e2be370d2c Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 20 Nov 2014 22:12:21 +0100 Subject: [PATCH] Added to each Context a reference to the Engine. It's only available during the rendering. --- django/template/base.py | 9 +++++++++ django/template/context.py | 7 ++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/django/template/base.py b/django/template/base.py index c8c9844a6a..ef611bc954 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -150,11 +150,19 @@ class Template(object): def render(self, context): "Display stage -- can be called many times" + # Set engine attribute here to avoid changing the signature of either + # Context.__init__ or Node.render. The engine is set only on the first + # call to render. Further calls e.g. for includes don't override it. + toplevel_render = context.engine is None + if toplevel_render: + context.engine = self.engine context.render_context.push() try: return self._render(context) finally: context.render_context.pop() + if toplevel_render: + context.engine = None def compile_string(template_string, origin): @@ -1236,6 +1244,7 @@ class Library(object): 'current_app': context.current_app, 'use_l10n': context.use_l10n, 'use_tz': context.use_tz, + 'engine': context.engine, }) # Copy across the CSRF token, if present, because # inclusion tags are often used for forms, and we need diff --git a/django/template/context.py b/django/template/context.py index 5d66dd19fd..e48cfaef3b 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -121,11 +121,12 @@ class BaseContext(object): class Context(BaseContext): "A stack container for variable context" def __init__(self, dict_=None, autoescape=True, current_app=None, - use_l10n=None, use_tz=None): + use_l10n=None, use_tz=None, engine=None): self.autoescape = autoescape self.current_app = current_app self.use_l10n = use_l10n self.use_tz = use_tz + self.engine = engine self.render_context = RenderContext() super(Context, self).__init__(dict_) @@ -186,9 +187,9 @@ class RequestContext(Context): using the "processors" keyword argument. """ def __init__(self, request, dict_=None, processors=None, current_app=None, - use_l10n=None, use_tz=None): + use_l10n=None, use_tz=None, engine=None): Context.__init__(self, dict_, current_app=current_app, - use_l10n=use_l10n, use_tz=use_tz) + use_l10n=use_l10n, use_tz=use_tz, engine=engine) if processors is None: processors = () else: