Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Reviewed by Loic Bistuer and Tim Graham.
  • Loading branch information...
commit 5cdacbda034af928f5033c9afc7b50ee0b13f75c 1 parent 1696376
@funkybob funkybob authored akaariai committed
View
7 django/template/loader_tags.py
@@ -159,8 +159,11 @@ def __init__(self, template_name, *args, **kwargs):
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
6 docs/ref/templates/builtins.txt
@@ -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
5 docs/releases/1.7.txt
@@ -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
11 tests/template_tests/tests.py
@@ -338,6 +338,17 @@ def test_extends_include_missing_cachedloader(self):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.