Skip to content

Commit

Permalink
Fixed #17356 -- Allowed {% include %} to render compiled templates
Browse files Browse the repository at this point in the history
Reviewed by Loic Bistuer and Tim Graham.
  • Loading branch information
funkybob authored and akaariai committed Aug 29, 2013
1 parent 1696376 commit 5cdacbd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions django/template/loader_tags.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ def __init__(self, template_name, *args, **kwargs):


def render(self, context): def render(self, context):
try: try:
template_name = self.template_name.resolve(context) template = self.template_name.resolve(context)
template = get_template(template_name) # Does this quack like a Template?
if not callable(getattr(template, 'render', None)):
# If not, we'll try get_template
template = get_template(template)
return self.render_template(template, context) return self.render_template(template, context)
except: except:
if settings.TEMPLATE_DEBUG: if settings.TEMPLATE_DEBUG:
Expand Down
6 changes: 6 additions & 0 deletions docs/ref/templates/builtins.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -691,6 +691,12 @@ the variable ``template_name``::


{% include template_name %} {% include template_name %}


.. versionchanged:: 1.7

The variable may also be any object with a ``render()`` method that
accepts a context. This allows you to reference a compiled ``Template`` in
your context.

An included template is rendered with the context of the template that's An included template is rendered with the context of the template that's
including it. This example produces the output ``"Hello, John"``: including it. This example produces the output ``"Hello, John"``:


Expand Down
5 changes: 5 additions & 0 deletions docs/releases/1.7.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ Templates
* The :ttag:`widthratio` template tag now accepts an "as" parameter to capture * The :ttag:`widthratio` template tag now accepts an "as" parameter to capture
the result in a variable. the result in a variable.


* The :ttag:`include` template tag will now also accept anything with a
``render()`` method (such as a ``Template``) as an argument. String
arguments will be looked up using
:func:`~django.template.loader.get_template` as always.

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


Expand Down
11 changes: 11 additions & 0 deletions tests/template_tests/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ def test_extends_include_missing_cachedloader(self):
loader.template_source_loaders = old_loaders loader.template_source_loaders = old_loaders
settings.TEMPLATE_DEBUG = old_td settings.TEMPLATE_DEBUG = old_td


def test_include_template_argument(self):
"""
Support any render() supporting object
"""
ctx = Context({
'tmpl': Template('This worked!'),
})
outer_tmpl = Template('{% include tmpl %}')
output = outer_tmpl.render(ctx)
self.assertEqual(output, 'This worked!')



class TemplateRegressionTests(TestCase): class TemplateRegressionTests(TestCase):


Expand Down

0 comments on commit 5cdacbd

Please sign in to comment.