Fix Jinj2 to include_block not including local variables

pull/6854/head
Jonny Scholes 2020-08-12 13:18:04 +10:00 zatwierdzone przez Karl Hobley
rodzic ca44453c2f
commit 268b2991ed
5 zmienionych plików z 20 dodań i 7 usunięć

Wyświetl plik

@ -44,7 +44,7 @@ testing_extras = [
'python-dateutil>=2.2',
'pytz>=2014.7',
'elasticsearch>=5.0,<6.0',
'Jinja2>=2.8,<3.0',
'Jinja2>=2.11,<3.0',
'boto3>=1.16,<1.17',
'freezegun>=0.3.8',
'openpyxl>=2.6.4',

Wyświetl plik

@ -37,7 +37,11 @@ class WagtailCoreExtension(Extension):
parser.stream.skip()
if with_context:
args.append(jinja2.nodes.ContextReference())
if hasattr(jinja2.nodes, 'DerivedContextReference'):
# DerivedContextReference includes local variables. Introduced in Jinja 2.11
args.append(jinja2.nodes.DerivedContextReference())
else:
args.append(jinja2.nodes.ContextReference())
else:
# Actually we can just skip else branch because context arg default to None
args.append(jinja2.nodes.Const(None))

Wyświetl plik

@ -182,3 +182,16 @@ class TestIncludeBlockTag(TestCase):
'language': 'fr',
})
self.assertIn('<body>999</body>', result)
def test_include_block_tag_with_additional_variable(self):
"""
The include_block tag should be able to pass local variables from parent context to the
child context
"""
block = blocks.CharBlock(template='tests/blocks/heading_block.html')
bound_block = block.bind('bonjour')
result = render_to_string('tests/jinja2/include_block_tag_with_additional_variable.html', {
'test_block': bound_block
})
self.assertIn('<body><h1 class="important">bonjour</h1></body>', result)

Wyświetl plik

@ -1,5 +0,0 @@
{% with classname = 'asdf' %}
{{ classname }}
{{ language }}
<body>{% include_block test_block with context %}</body>
{% endwith %}

Wyświetl plik

@ -0,0 +1 @@
<body>{% with classname="important" %}{% include_block test_block with context %}{% endwith %}</body>