Code

Ticket #17356: 17356.diff

File 17356.diff, 2.5 KB (added by SmileyChris, 2 years ago)

Patch with test & docs

Line 
1diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py
2index b642765..0abd215 100644
3--- a/django/template/loader_tags.py
4+++ b/django/template/loader_tags.py
5@@ -165,7 +165,11 @@ class IncludeNode(BaseIncludeNode):
6     def render(self, context):
7         try:
8             template_name = self.template_name.resolve(context)
9-            template = get_template(template_name)
10+            if hasattr(template_name, 'render'):
11+                # The variable itself is a template, no need to get find it.
12+                template = template_name
13+            else:
14+                template = get_template(template_name)
15             return self.render_template(template, context)
16         except:
17             if settings.TEMPLATE_DEBUG:
18diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
19index 4e5c124..ddf1954 100644
20--- a/docs/ref/templates/builtins.txt
21+++ b/docs/ref/templates/builtins.txt
22@@ -657,6 +657,12 @@ the variable ``template_name``::
23 
24     {% include template_name %}
25 
26+.. versionchanged:: 1.4
27+   Render templates passed directly in context.
28+
29+You can also include the contents of a compiled
30+:class:`~django.template.Template` that is on the template context.
31+
32 An included template is rendered with the context of the template that's
33 including it. This example produces the output ``"Hello, John"``:
34 
35diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
36index 712093b..b7052cf 100644
37--- a/tests/regressiontests/templates/tests.py
38+++ b/tests/regressiontests/templates/tests.py
39@@ -1046,6 +1046,9 @@ class Templates(unittest.TestCase):
40             'include13': ('{% autoescape off %}{% include "basic-syntax03" %}{% endautoescape %}', {'first': '&'}, ('& --- ', '& --- INVALID')),
41             'include14': ('{% autoescape off %}{% include "basic-syntax03" with first=var1 only %}{% endautoescape %}', {'var1': '&'}, ('& --- ', '& --- INVALID')),
42 
43+            # render template passed to context
44+            'include15': ('{% include tmpl %}', {'tmpl': template.Template('{{ a }} --- {{ b }}'), 'a': 'A'}, ('A --- ', 'A --- INVALID')),
45+
46             'include-error01': ('{% include "basic-syntax01" with %}', {}, template.TemplateSyntaxError),
47             'include-error02': ('{% include "basic-syntax01" with "no key" %}', {}, template.TemplateSyntaxError),
48             'include-error03': ('{% include "basic-syntax01" with dotted.arg="error" %}', {}, template.TemplateSyntaxError),