Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refs #2333 - Added a signal that is emitted whenever a template is re…

…ndered, and added a 'name' field to Template to allow easy identification of templates.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3659 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 89fa97b8374131614698bbab1544f9bdafdb66bd 1 parent 7dce86c
Russell Keith-Magee freakboy3742 authored
6 django/template/__init__.py
View
@@ -60,6 +60,8 @@
from django.template.context import Context, RequestContext, ContextPopException
from django.utils.functional import curry
from django.utils.text import smart_split
+from django.dispatch import dispatcher
+from django.template import signals
__all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
@@ -137,13 +139,14 @@ def reload(self):
return self.source
class Template(object):
- def __init__(self, template_string, origin=None):
+ def __init__(self, template_string, origin=None, name='<Unknown Template>'):
"Compilation stage"
if settings.TEMPLATE_DEBUG and origin == None:
origin = StringOrigin(template_string)
# Could do some crazy stack-frame stuff to record where this string
# came from...
self.nodelist = compile_string(template_string, origin)
+ self.name = name
def __iter__(self):
for node in self.nodelist:
@@ -152,6 +155,7 @@ def __iter__(self):
def render(self, context):
"Display stage -- can be called many times"
+ dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context)
return self.nodelist.render(context)
def compile_string(template_string, origin):
2  django/template/defaulttags.py
View
@@ -251,7 +251,7 @@ def render(self, context):
output = ''
if self.parsed:
try:
- t = Template(output)
+ t = Template(output, name=self.filepath)
return t.render(context)
except TemplateSyntaxError, e:
if settings.DEBUG:
8 django/template/loader.py
View
@@ -76,14 +76,16 @@ def get_template(template_name):
Returns a compiled Template object for the given template name,
handling template inheritance recursively.
"""
- return get_template_from_string(*find_template_source(template_name))
+ source, origin = find_template_source(template_name)
+ template = get_template_from_string(source, origin, template_name)
+ return template
-def get_template_from_string(source, origin=None):
+def get_template_from_string(source, origin=None, name=None):
"""
Returns a compiled Template object for the given template code,
handling template inheritance recursively.
"""
- return Template(source, origin)
+ return Template(source, origin, name)
def render_to_string(template_name, dictionary=None, context_instance=None):
"""
2  django/template/loader_tags.py
View
@@ -57,7 +57,7 @@ def get_parent(self, context):
except TemplateDoesNotExist:
raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
else:
- return get_template_from_string(source, origin)
+ return get_template_from_string(source, origin, parent)
def render(self, context):
compiled_parent = self.get_parent(context)
1  django/template/signals.py
View
@@ -0,0 +1 @@
+template_rendered=object()
6 django/views/debug.py
View
@@ -115,7 +115,7 @@ def technical_500_response(request, exc_type, exc_value, tb):
'function': '?',
'lineno': '?',
}]
- t = Template(TECHNICAL_500_TEMPLATE)
+ t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 Template')
c = Context({
'exception_type': exc_type.__name__,
'exception_value': exc_value,
@@ -141,7 +141,7 @@ def technical_404_response(request, exception):
# tried exists but is an empty list. The URLconf must've been empty.
return empty_urlconf(request)
- t = Template(TECHNICAL_404_TEMPLATE)
+ t = Template(TECHNICAL_404_TEMPLATE, name='Technical 404 Template')
c = Context({
'root_urlconf': settings.ROOT_URLCONF,
'urlpatterns': tried,
@@ -154,7 +154,7 @@ def technical_404_response(request, exception):
def empty_urlconf(request):
"Create an empty URLconf 404 error response."
- t = Template(EMPTY_URLCONF_TEMPLATE)
+ t = Template(EMPTY_URLCONF_TEMPLATE, name='Empty URLConf Template')
c = Context({
'project_name': settings.SETTINGS_MODULE.split('.')[0]
})
2  django/views/static.py
View
@@ -81,7 +81,7 @@ def directory_index(path, fullpath):
try:
t = loader.get_template('static/directory_index')
except TemplateDoesNotExist:
- t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE)
+ t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE, name='Default Directory Index Template')
files = []
for f in os.listdir(fullpath):
if not f.startswith('.'):
Please sign in to comment.
Something went wrong with that request. Please try again.