Encapsulated TEMPLATE_DEBUG in Engine.

This commit is contained in:
Aymeric Augustin 2014-11-22 21:33:40 +01:00
parent 47a131b944
commit 6294bd3903
5 changed files with 10 additions and 9 deletions

View File

@ -7,7 +7,6 @@ from inspect import getargspec, getcallargs
import warnings import warnings
from django.apps import apps from django.apps import apps
from django.conf import settings
from django.template.context import (BaseContext, Context, RequestContext, # NOQA: imported for backwards compatibility from django.template.context import (BaseContext, Context, RequestContext, # NOQA: imported for backwards compatibility
ContextPopException) ContextPopException)
from django.utils import lru_cache from django.utils import lru_cache
@ -126,11 +125,11 @@ class Template(object):
except UnicodeDecodeError: except UnicodeDecodeError:
raise TemplateEncodingError("Templates can only be constructed " raise TemplateEncodingError("Templates can only be constructed "
"from unicode or UTF-8 strings.") "from unicode or UTF-8 strings.")
if settings.TEMPLATE_DEBUG and origin is None:
origin = StringOrigin(template_string)
if engine is None: if engine is None:
from .engine import Engine from .engine import Engine
engine = Engine.get_default() engine = Engine.get_default()
if engine.debug and origin is None:
origin = StringOrigin(template_string)
self.nodelist = engine.compile_string(template_string, origin) self.nodelist = engine.compile_string(template_string, origin)
self.name = name self.name = name
self.origin = origin self.origin = origin

View File

@ -210,7 +210,7 @@ class ForNode(Node):
context[self.loopvars[0]] = item context[self.loopvars[0]] = item
# In TEMPLATE_DEBUG mode provide source of the node which # In TEMPLATE_DEBUG mode provide source of the node which
# actually raised the exception # actually raised the exception
if settings.TEMPLATE_DEBUG: if context.engine.debug:
for node in self.nodelist_loop: for node in self.nodelist_loop:
try: try:
nodelist.append(node.render(context)) nodelist.append(node.render(context))

View File

@ -19,7 +19,7 @@ class Engine(object):
def __init__(self, dirs=None, app_dirs=False, def __init__(self, dirs=None, app_dirs=False,
allowed_include_roots=None, context_processors=None, allowed_include_roots=None, context_processors=None,
loaders=None, string_if_invalid='', debug=False, loaders=None, string_if_invalid='',
file_charset=None): file_charset=None):
if dirs is None: if dirs is None:
dirs = [] dirs = []
@ -42,6 +42,7 @@ class Engine(object):
self.app_dirs = app_dirs self.app_dirs = app_dirs
self.allowed_include_roots = allowed_include_roots self.allowed_include_roots = allowed_include_roots
self.context_processors = context_processors self.context_processors = context_processors
self.debug = debug
self.loaders = loaders self.loaders = loaders
self.string_if_invalid = string_if_invalid self.string_if_invalid = string_if_invalid
self.file_charset = file_charset self.file_charset = file_charset
@ -54,6 +55,7 @@ class Engine(object):
dirs=settings.TEMPLATE_DIRS, dirs=settings.TEMPLATE_DIRS,
allowed_include_roots=settings.ALLOWED_INCLUDE_ROOTS, allowed_include_roots=settings.ALLOWED_INCLUDE_ROOTS,
context_processors=settings.TEMPLATE_CONTEXT_PROCESSORS, context_processors=settings.TEMPLATE_CONTEXT_PROCESSORS,
debug=settings.TEMPLATE_DEBUG,
loaders=settings.TEMPLATE_LOADERS, loaders=settings.TEMPLATE_LOADERS,
string_if_invalid=settings.TEMPLATE_STRING_IF_INVALID, string_if_invalid=settings.TEMPLATE_STRING_IF_INVALID,
file_charset=settings.FILE_CHARSET, file_charset=settings.FILE_CHARSET,
@ -211,7 +213,7 @@ class Engine(object):
""" """
Compiles template_string into a NodeList ready for rendering. Compiles template_string into a NodeList ready for rendering.
""" """
if settings.TEMPLATE_DEBUG: if self.debug:
from .debug import DebugLexer, DebugParser from .debug import DebugLexer, DebugParser
lexer_class, parser_class = DebugLexer, DebugParser lexer_class, parser_class = DebugLexer, DebugParser
else: else:
@ -222,7 +224,7 @@ class Engine(object):
return parser.parse() return parser.parse()
def make_origin(self, display_name, loader, name, dirs): def make_origin(self, display_name, loader, name, dirs):
if settings.TEMPLATE_DEBUG and display_name: if self.debug and display_name:
# Inner import to avoid circular dependency # Inner import to avoid circular dependency
from .loader import LoaderOrigin from .loader import LoaderOrigin
return LoaderOrigin(display_name, loader, name, dirs) return LoaderOrigin(display_name, loader, name, dirs)

View File

@ -1,6 +1,5 @@
from collections import defaultdict from collections import defaultdict
from django.conf import settings
from django.template.base import TemplateSyntaxError, Library, Node, TextNode,\ from django.template.base import TemplateSyntaxError, Library, Node, TextNode,\
token_kwargs, Variable token_kwargs, Variable
from django.template.loader import get_template from django.template.loader import get_template
@ -154,7 +153,7 @@ class IncludeNode(Node):
with context.push(**values): with context.push(**values):
return template.render(context) return template.render(context)
except Exception: except Exception:
if settings.TEMPLATE_DEBUG: if context.engine.debug:
raise raise
return '' return ''

View File

@ -83,6 +83,7 @@ def reset_default_template_engine(**kwargs):
'TEMPLATE_DIRS', 'TEMPLATE_DIRS',
'ALLOWED_INCLUDE_ROOTS', 'ALLOWED_INCLUDE_ROOTS',
'TEMPLATE_CONTEXT_PROCESSORS', 'TEMPLATE_CONTEXT_PROCESSORS',
'TEMPLATE_DEBUG',
'TEMPLATE_LOADERS', 'TEMPLATE_LOADERS',
'TEMPLATE_STRING_IF_INVALID', 'TEMPLATE_STRING_IF_INVALID',
'FILE_CHARSET', 'FILE_CHARSET',