Ticket #17356: 17356.diff

File 17356.diff, 2.5 KB (added by Chris Beaven, 13 years ago)

Patch with test & docs

  • django/template/loader_tags.py

    diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py
    index b642765..0abd215 100644
    a b class IncludeNode(BaseIncludeNode):  
    165165    def render(self, context):
    166166        try:
    167167            template_name = self.template_name.resolve(context)
    168             template = get_template(template_name)
     168            if hasattr(template_name, 'render'):
     169                # The variable itself is a template, no need to get find it.
     170                template = template_name
     171            else:
     172                template = get_template(template_name)
    169173            return self.render_template(template, context)
    170174        except:
    171175            if settings.TEMPLATE_DEBUG:
  • docs/ref/templates/builtins.txt

    diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
    index 4e5c124..ddf1954 100644
    a b the variable ``template_name``::  
    657657
    658658    {% include template_name %}
    659659
     660.. versionchanged:: 1.4
     661   Render templates passed directly in context.
     662
     663You can also include the contents of a compiled
     664:class:`~django.template.Template` that is on the template context.
     665
    660666An included template is rendered with the context of the template that's
    661667including it. This example produces the output ``"Hello, John"``:
    662668
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 712093b..b7052cf 100644
    a b class Templates(unittest.TestCase):  
    10461046            'include13': ('{% autoescape off %}{% include "basic-syntax03" %}{% endautoescape %}', {'first': '&'}, ('& --- ', '& --- INVALID')),
    10471047            'include14': ('{% autoescape off %}{% include "basic-syntax03" with first=var1 only %}{% endautoescape %}', {'var1': '&'}, ('& --- ', '& --- INVALID')),
    10481048
     1049            # render template passed to context
     1050            'include15': ('{% include tmpl %}', {'tmpl': template.Template('{{ a }} --- {{ b }}'), 'a': 'A'}, ('A --- ', 'A --- INVALID')),
     1051
    10491052            'include-error01': ('{% include "basic-syntax01" with %}', {}, template.TemplateSyntaxError),
    10501053            'include-error02': ('{% include "basic-syntax01" with "no key" %}', {}, template.TemplateSyntaxError),
    10511054            'include-error03': ('{% include "basic-syntax01" with dotted.arg="error" %}', {}, template.TemplateSyntaxError),
Back to Top