mirror of https://github.com/django/django.git
Fixed #17356 -- Allowed {% include %} to render compiled templates
Reviewed by Loic Bistuer and Tim Graham.
This commit is contained in:
parent
169637649b
commit
5cdacbda03
|
@ -159,8 +159,11 @@ class IncludeNode(BaseIncludeNode):
|
||||||
|
|
||||||
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:
|
||||||
|
|
|
@ -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"``:
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
=====================================
|
=====================================
|
||||||
|
|
||||||
|
|
|
@ -338,6 +338,17 @@ class TemplateLoaderTests(TestCase):
|
||||||
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):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue