summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCurtis Maloney <curtis@tinbrain.net>2013-08-28 22:17:20 +1000
committerAnssi Kääriäinen <akaariai@gmail.com>2013-08-29 10:22:24 +0300
commit5cdacbda034af928f5033c9afc7b50ee0b13f75c (patch)
tree5621b597d673fffbcca6c70fbdd3f132ec1b1f31
parent169637649baa012a8a77b17465c5c0c1085336ea (diff)
Fixed #17356 -- Allowed {% include %} to render compiled templates
Reviewed by Loic Bistuer and Tim Graham.
-rw-r--r--django/template/loader_tags.py7
-rw-r--r--docs/ref/templates/builtins.txt6
-rw-r--r--docs/releases/1.7.txt5
-rw-r--r--tests/template_tests/tests.py11
4 files changed, 27 insertions, 2 deletions
diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py
index d48f85eb35..224efe884b 100644
--- a/django/template/loader_tags.py
+++ b/django/template/loader_tags.py
@@ -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:
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index 5b02ab22d1..767906cd8b 100644
--- a/docs/ref/templates/builtins.txt
+++ b/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"``:
diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt
index 2bf7c0258f..d0c3be1688 100644
--- a/docs/releases/1.7.txt
+++ b/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
=====================================
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index 034280f08d..c2179b43c1 100644
--- a/tests/template_tests/tests.py
+++ b/tests/template_tests/tests.py
@@ -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):