Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16096 -- Added origin attribute to template instances.

Thanks jdunck for the suggestion.
  • Loading branch information...
commit 8625c7aab3760d16572792fea23a95e572e28ead 1 parent e1266e5
Preston Timmons authored August 30, 2013 timgraham committed September 06, 2013
1  django/template/base.py
@@ -124,6 +124,7 @@ def __init__(self, template_string, origin=None,
124 124
             origin = StringOrigin(template_string)
125 125
         self.nodelist = compile_string(template_string, origin)
126 126
         self.name = name
  127
+        self.origin = origin
127 128
 
128 129
     def __iter__(self):
129 130
         for node in self.nodelist:
37  docs/ref/templates/api.txt
@@ -759,10 +759,45 @@ Django uses the template loaders in order according to the
759 759
 :setting:`TEMPLATE_LOADERS` setting. It uses each loader until a loader finds a
760 760
 match.
761 761
 
  762
+.. currentmodule:: django.template
  763
+
  764
+Template origin
  765
+~~~~~~~~~~~~~~~
  766
+
  767
+.. versionadded:: 1.7
  768
+
  769
+When :setting:`TEMPLATE_DEBUG` is ``True`` template objects will have an
  770
+``origin`` attribute depending on the source they are loaded from.
  771
+
  772
+.. class:: loader.LoaderOrigin
  773
+
  774
+    Templates created from a template loader will use the
  775
+    ``django.template.loader.LoaderOrigin`` class.
  776
+
  777
+    .. attribute:: name
  778
+
  779
+        The path to the template as returned by the template loader.
  780
+        For loaders that read from the file system, this is the full
  781
+        path to the template.
  782
+
  783
+    .. attribute:: loadname
  784
+
  785
+        The relative path to the template as passed into the
  786
+        template loader.
  787
+
  788
+.. class:: StringOrigin
  789
+
  790
+    Templates created from a ``Template`` class will use the
  791
+    ``django.template.StringOrigin`` class.
  792
+
  793
+    .. attribute:: source
  794
+
  795
+        The string used to create the template.
  796
+
762 797
 The ``render_to_string`` shortcut
763 798
 ===================================
764 799
 
765  
-.. function:: django.template.loader.render_to_string(template_name, dictionary=None, context_instance=None)
  800
+.. function:: loader.render_to_string(template_name, dictionary=None, context_instance=None)
766 801
 
767 802
 To cut down on the repetitive nature of loading and rendering
768 803
 templates, Django provides a shortcut function which largely
4  docs/releases/1.7.txt
@@ -272,6 +272,10 @@ Templates
272 272
 
273 273
 * It is now possible to :ttag:`include` templates recursively.
274 274
 
  275
+* Template objects now have an origin attribute set when
  276
+  :setting:`TEMPLATE_DEBUG` is ``True``. This allows template origins to be
  277
+  inspected and logged outside of the ``django.template`` infrastructure.
  278
+
275 279
 Backwards incompatible changes in 1.7
276 280
 =====================================
277 281
 
13  tests/template_tests/tests.py
@@ -236,6 +236,19 @@ def test_loader_debug_origin(self):
236 236
             loader.template_source_loaders = old_loaders
237 237
             settings.TEMPLATE_DEBUG = old_td
238 238
 
  239
+    def test_loader_origin(self):
  240
+        with self.settings(TEMPLATE_DEBUG=True):
  241
+            template = loader.get_template('login.html')
  242
+            self.assertEqual(template.origin.loadname, 'login.html')
  243
+
  244
+    def test_string_origin(self):
  245
+        with self.settings(TEMPLATE_DEBUG=True):
  246
+            template = Template('string template')
  247
+            self.assertEqual(template.origin.source, 'string template')
  248
+
  249
+    def test_debug_false_origin(self):
  250
+        template = loader.get_template('login.html')
  251
+        self.assertEqual(template.origin, None)
239 252
 
240 253
     def test_include_missing_template(self):
241 254
         """

0 notes on commit 8625c7a

Please sign in to comment.
Something went wrong with that request. Please try again.