Skip to content

Commit

Permalink
Fixed #16096 -- Added origin attribute to template instances.
Browse files Browse the repository at this point in the history
Thanks jdunck for the suggestion.
  • Loading branch information
prestontimmons authored and timgraham committed Sep 6, 2013
1 parent e1266e5 commit 8625c7a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions django/template/base.py
Expand Up @@ -124,6 +124,7 @@ def __init__(self, template_string, origin=None,
origin = StringOrigin(template_string)
self.nodelist = compile_string(template_string, origin)
self.name = name
self.origin = origin

def __iter__(self):
for node in self.nodelist:
Expand Down
37 changes: 36 additions & 1 deletion docs/ref/templates/api.txt
Expand Up @@ -759,10 +759,45 @@ Django uses the template loaders in order according to the
:setting:`TEMPLATE_LOADERS` setting. It uses each loader until a loader finds a
match.

.. currentmodule:: django.template

Template origin
~~~~~~~~~~~~~~~

.. versionadded:: 1.7

When :setting:`TEMPLATE_DEBUG` is ``True`` template objects will have an
``origin`` attribute depending on the source they are loaded from.

.. class:: loader.LoaderOrigin

Templates created from a template loader will use the
``django.template.loader.LoaderOrigin`` class.

.. attribute:: name

The path to the template as returned by the template loader.
For loaders that read from the file system, this is the full
path to the template.

.. attribute:: loadname

The relative path to the template as passed into the
template loader.

.. class:: StringOrigin

Templates created from a ``Template`` class will use the
``django.template.StringOrigin`` class.

.. attribute:: source

The string used to create the template.

The ``render_to_string`` shortcut
===================================

.. function:: django.template.loader.render_to_string(template_name, dictionary=None, context_instance=None)
.. function:: loader.render_to_string(template_name, dictionary=None, context_instance=None)

To cut down on the repetitive nature of loading and rendering
templates, Django provides a shortcut function which largely
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/1.7.txt
Expand Up @@ -272,6 +272,10 @@ Templates

* It is now possible to :ttag:`include` templates recursively.

* Template objects now have an origin attribute set when
:setting:`TEMPLATE_DEBUG` is ``True``. This allows template origins to be
inspected and logged outside of the ``django.template`` infrastructure.

Backwards incompatible changes in 1.7
=====================================

Expand Down
13 changes: 13 additions & 0 deletions tests/template_tests/tests.py
Expand Up @@ -236,6 +236,19 @@ def test_loader_debug_origin(self):
loader.template_source_loaders = old_loaders
settings.TEMPLATE_DEBUG = old_td

def test_loader_origin(self):
with self.settings(TEMPLATE_DEBUG=True):
template = loader.get_template('login.html')
self.assertEqual(template.origin.loadname, 'login.html')

def test_string_origin(self):
with self.settings(TEMPLATE_DEBUG=True):
template = Template('string template')
self.assertEqual(template.origin.source, 'string template')

def test_debug_false_origin(self):
template = loader.get_template('login.html')
self.assertEqual(template.origin, None)

def test_include_missing_template(self):
"""
Expand Down

0 comments on commit 8625c7a

Please sign in to comment.