Fixed #17356 -- Allowed {% include %} to render compiled templates

Reviewed by Loic Bistuer and Tim Graham.
This commit is contained in:
Curtis Maloney 2013-08-28 22:17:20 +10:00 committed by Anssi Kääriäinen
parent 169637649b
commit 5cdacbda03
4 changed files with 27 additions and 2 deletions

View File

@ -159,8 +159,11 @@ class IncludeNode(BaseIncludeNode):
def render(self, context):
try:
template_name = self.template_name.resolve(context)
template = get_template(template_name)
template = self.template_name.resolve(context)
# 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)
except:
if settings.TEMPLATE_DEBUG:

View File

@ -691,6 +691,12 @@ the variable ``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
including it. This example produces the output ``"Hello, John"``:

View File

@ -258,6 +258,11 @@ Templates
* The :ttag:`widthratio` template tag now accepts an "as" parameter to capture
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
=====================================

View File

@ -338,6 +338,17 @@ class TemplateLoaderTests(TestCase):
loader.template_source_loaders = old_loaders
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):