Skip to content

Commit 89fa97b

Browse files
committed
Refs django#2333 - Added a signal that is emitted whenever a template is rendered, 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
1 parent 7dce86c commit 89fa97b

File tree

7 files changed

+17
-10
lines changed

7 files changed

+17
-10
lines changed

django/template/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
from django.template.context import Context, RequestContext, ContextPopException
6161
from django.utils.functional import curry
6262
from django.utils.text import smart_split
63+
from django.dispatch import dispatcher
64+
from django.template import signals
6365

6466
__all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
6567

@@ -137,13 +139,14 @@ def reload(self):
137139
return self.source
138140

139141
class Template(object):
140-
def __init__(self, template_string, origin=None):
142+
def __init__(self, template_string, origin=None, name='<Unknown Template>'):
141143
"Compilation stage"
142144
if settings.TEMPLATE_DEBUG and origin == None:
143145
origin = StringOrigin(template_string)
144146
# Could do some crazy stack-frame stuff to record where this string
145147
# came from...
146148
self.nodelist = compile_string(template_string, origin)
149+
self.name = name
147150

148151
def __iter__(self):
149152
for node in self.nodelist:
@@ -152,6 +155,7 @@ def __iter__(self):
152155

153156
def render(self, context):
154157
"Display stage -- can be called many times"
158+
dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context)
155159
return self.nodelist.render(context)
156160

157161
def compile_string(template_string, origin):

django/template/defaulttags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def render(self, context):
251251
output = ''
252252
if self.parsed:
253253
try:
254-
t = Template(output)
254+
t = Template(output, name=self.filepath)
255255
return t.render(context)
256256
except TemplateSyntaxError, e:
257257
if settings.DEBUG:

django/template/loader.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ def get_template(template_name):
7676
Returns a compiled Template object for the given template name,
7777
handling template inheritance recursively.
7878
"""
79-
return get_template_from_string(*find_template_source(template_name))
79+
source, origin = find_template_source(template_name)
80+
template = get_template_from_string(source, origin, template_name)
81+
return template
8082

81-
def get_template_from_string(source, origin=None):
83+
def get_template_from_string(source, origin=None, name=None):
8284
"""
8385
Returns a compiled Template object for the given template code,
8486
handling template inheritance recursively.
8587
"""
86-
return Template(source, origin)
88+
return Template(source, origin, name)
8789

8890
def render_to_string(template_name, dictionary=None, context_instance=None):
8991
"""

django/template/loader_tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def get_parent(self, context):
5757
except TemplateDoesNotExist:
5858
raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
5959
else:
60-
return get_template_from_string(source, origin)
60+
return get_template_from_string(source, origin, parent)
6161

6262
def render(self, context):
6363
compiled_parent = self.get_parent(context)

django/template/signals.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
template_rendered=object()

django/views/debug.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def technical_500_response(request, exc_type, exc_value, tb):
115115
'function': '?',
116116
'lineno': '?',
117117
}]
118-
t = Template(TECHNICAL_500_TEMPLATE)
118+
t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 Template')
119119
c = Context({
120120
'exception_type': exc_type.__name__,
121121
'exception_value': exc_value,
@@ -141,7 +141,7 @@ def technical_404_response(request, exception):
141141
# tried exists but is an empty list. The URLconf must've been empty.
142142
return empty_urlconf(request)
143143

144-
t = Template(TECHNICAL_404_TEMPLATE)
144+
t = Template(TECHNICAL_404_TEMPLATE, name='Technical 404 Template')
145145
c = Context({
146146
'root_urlconf': settings.ROOT_URLCONF,
147147
'urlpatterns': tried,
@@ -154,7 +154,7 @@ def technical_404_response(request, exception):
154154

155155
def empty_urlconf(request):
156156
"Create an empty URLconf 404 error response."
157-
t = Template(EMPTY_URLCONF_TEMPLATE)
157+
t = Template(EMPTY_URLCONF_TEMPLATE, name='Empty URLConf Template')
158158
c = Context({
159159
'project_name': settings.SETTINGS_MODULE.split('.')[0]
160160
})

django/views/static.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def directory_index(path, fullpath):
8181
try:
8282
t = loader.get_template('static/directory_index')
8383
except TemplateDoesNotExist:
84-
t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE)
84+
t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE, name='Default Directory Index Template')
8585
files = []
8686
for f in os.listdir(fullpath):
8787
if not f.startswith('.'):

0 commit comments

Comments
 (0)