Improved {% include %} implementation

Merged BaseIncludeNode, ConstantIncludeNode and Include node.

This avoids raising TemplateDoesNotExist at parsing time, allows recursion
when passing a literal template name, and should make TEMPLATE_DEBUG behavior
consistant.

Thanks loic84 for help with the tests.

Fixed #3544, fixed #12064, fixed #16147
This commit is contained in:
Curtis Maloney 2013-08-29 18:58:56 +10:00 committed by Anssi Kääriäinen
parent e973ee6a98
commit e2f06226ea
4 changed files with 56 additions and 38 deletions

View file

@ -349,6 +349,40 @@ class TemplateLoaderTests(TestCase):
output = outer_tmpl.render(ctx)
self.assertEqual(output, 'This worked!')
@override_settings(TEMPLATE_DEBUG=True)
def test_include_immediate_missing(self):
"""
Regression test for #16417 -- {% include %} tag raises TemplateDoesNotExist at compile time if TEMPLATE_DEBUG is True
Test that an {% include %} tag with a literal string referencing a
template that does not exist does not raise an exception at parse
time.
"""
ctx = Context()
tmpl = Template('{% include "this_does_not_exist.html" %}')
self.assertIsInstance(tmpl, Template)
@override_settings(TEMPLATE_DEBUG=True)
def test_include_recursive(self):
comments = [
{
'comment': 'A1',
'children': [
{'comment': 'B1', 'children': []},
{'comment': 'B2', 'children': []},
{'comment': 'B3', 'children': [
{'comment': 'C1', 'children': []}
]},
]
}
]
t = loader.get_template('recursive_include.html')
self.assertEqual(
"Recursion! A1 Recursion! B1 B2 B3 Recursion! C1",
t.render(Context({'comments': comments})).replace(' ', '').replace('\n', ' ').strip(),
)
class TemplateRegressionTests(TestCase):