Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support included blocks override

  • Loading branch information...
commit 33aee12a29426987805420a410e13e0470a79cbf 1 parent 80b4fe6
Mounier Florian authored
Showing with 25 additions and 2 deletions.
  1. +9 −2 jinja2/compiler.py
  2. +16 −0 jinja2/testsuite/imports.py
11 jinja2/compiler.py
View
@@ -950,9 +950,16 @@ def visit_Include(self, node, frame):
self.indent()
if node.with_context:
+ self.writeline('include_context = template.new_context('
+ 'context.parent, True, locals())')
+ self.writeline('for name, context_blocks in context.'
+ 'blocks.%s():' % dict_item_iter)
+ self.indent()
+ self.writeline('include_context.blocks.setdefault('
+ 'name, [])[0:0] = context_blocks')
+ self.outdent()
self.writeline('for event in template.root_render_func('
- 'template.new_context(context.parent, True, '
- 'locals())):')
+ 'include_context):')
else:
self.writeline('for event in template.module._body_stream:')
16 jinja2/testsuite/imports.py
View
@@ -121,6 +121,22 @@ def test_context_include_with_overrides(self):
)))
assert env.get_template("main").render() == "123"
+ def test_included_block_override(self):
+ env = Environment(loader=DictLoader(dict(
+ main="{% extends 'base' %}{% block b %}1337{% endblock %}",
+ base="{% include 'inc' %}",
+ inc="{% block b %}42{% endblock %}"
+ )))
+ assert env.get_template("main").render() == "1337"
+
+ def test_included_block_override_with_super(self):
+ env = Environment(loader=DictLoader(dict(
+ main="{% extends 'base' %}{% block b %}1337|{{ super() }}{% endblock %}",
+ base="{% include 'inc' %}",
+ inc="{% block b %}42{% endblock %}"
+ )))
+ assert env.get_template("main").render() == "1337|42"
+
def test_unoptimized_scopes(self):
t = test_env.from_string("""
{% macro outer(o) %}
Please sign in to comment.
Something went wrong with that request. Please try again.