Added to each Context a reference to the Engine.

It's only available during the rendering.
This commit is contained in:
Aymeric Augustin 2014-11-20 22:12:21 +01:00
parent a2dd08666c
commit 5b1bb40216
2 changed files with 13 additions and 3 deletions

View File

@ -150,11 +150,19 @@ class Template(object):
def render(self, context): def render(self, context):
"Display stage -- can be called many times" "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() context.render_context.push()
try: try:
return self._render(context) return self._render(context)
finally: finally:
context.render_context.pop() context.render_context.pop()
if toplevel_render:
context.engine = None
def compile_string(template_string, origin): def compile_string(template_string, origin):
@ -1236,6 +1244,7 @@ class Library(object):
'current_app': context.current_app, 'current_app': context.current_app,
'use_l10n': context.use_l10n, 'use_l10n': context.use_l10n,
'use_tz': context.use_tz, 'use_tz': context.use_tz,
'engine': context.engine,
}) })
# Copy across the CSRF token, if present, because # Copy across the CSRF token, if present, because
# inclusion tags are often used for forms, and we need # inclusion tags are often used for forms, and we need

View File

@ -121,11 +121,12 @@ class BaseContext(object):
class Context(BaseContext): class Context(BaseContext):
"A stack container for variable context" "A stack container for variable context"
def __init__(self, dict_=None, autoescape=True, current_app=None, 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.autoescape = autoescape
self.current_app = current_app self.current_app = current_app
self.use_l10n = use_l10n self.use_l10n = use_l10n
self.use_tz = use_tz self.use_tz = use_tz
self.engine = engine
self.render_context = RenderContext() self.render_context = RenderContext()
super(Context, self).__init__(dict_) super(Context, self).__init__(dict_)
@ -186,9 +187,9 @@ class RequestContext(Context):
using the "processors" keyword argument. using the "processors" keyword argument.
""" """
def __init__(self, request, dict_=None, processors=None, current_app=None, 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, 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: if processors is None:
processors = () processors = ()
else: else: