Skip to content

Commit

Permalink
Support new scoping rules in scoped blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jan 8, 2017
1 parent 31e0024 commit b1a56de
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Version 2.9.3
intended behavior in all situations however it does not restore the
old behavior where limited assignments to outer scopes was possible.
For more information and a discussion see #641
- Resolved an issue where `block scoped` would not take advantage of the
new scoping rules. In some more exotic cases a variable overriden in a
local scope would not make it into a block.

Version 2.9.2
-------------
Expand Down
8 changes: 5 additions & 3 deletions jinja2/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,12 @@ def call(__self, __obj, *args, **kwargs):
'StopIteration exception')

def derived(self, locals=None):
"""Internal helper function to create a derived context."""
"""Internal helper function to create a derived context. This is
used in situations where the system needs a new context in the same
template that is independent.
"""
context = new_context(self.environment, self.name, {},
self.parent, True, None, locals)
context.vars.update(self.vars)
self.get_all(), True, None, locals)
context.eval_ctx = self.eval_ctx
context.blocks.update((k, list(v)) for k, v in iteritems(self.blocks))
return context
Expand Down
5 changes: 5 additions & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,8 @@ def test_macro_blocks(self, env):
t = env.from_string('{% macro x() %}{% block foo %}x{% '
'endblock %}{% endmacro %}{{ x() }}')
assert t.render() == 'x'

def test_scoped_block(self, env):
t = env.from_string('{% set x = 1 %}{% with x = 2 %}{% block y scoped %}'
'{{ x }}{% endblock %}{% endwith %}')
assert t.render() == '2'

0 comments on commit b1a56de

Please sign in to comment.