From 5f3d6618748a5402236c1c4cfc06238a6e23a15d Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 22 Dec 2010 23:22:08 +0100 Subject: [PATCH] Fixed imports with scoped blocks --- CHANGES | 2 ++ jinja2/runtime.py | 1 + jinja2/testsuite/inheritance.py | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/CHANGES b/CHANGES index 87f705c..b06192c 100644 --- a/CHANGES +++ b/CHANGES @@ -15,6 +15,8 @@ Version 2.6 integers instead of longs) - groupby filter now supports dotted notation for grouping by attributes of attributes. +- scoped blocks not properly treat toplevel assignments and imports. + Previously an import suddenly "disappeared" in a scoped block. Version 2.5.5 ------------- diff --git a/jinja2/runtime.py b/jinja2/runtime.py index 9bdeb01..72ab93e 100644 --- a/jinja2/runtime.py +++ b/jinja2/runtime.py @@ -190,6 +190,7 @@ class Context(object): """Internal helper function to create a derived context.""" context = new_context(self.environment, self.name, {}, self.parent, True, None, locals) + context.vars.update(self.vars) context.eval_ctx = self.eval_ctx context.blocks.update((k, list(v)) for k, v in self.blocks.iteritems()) return context diff --git a/jinja2/testsuite/inheritance.py b/jinja2/testsuite/inheritance.py index 5db1cd6..355aa0c 100644 --- a/jinja2/testsuite/inheritance.py +++ b/jinja2/testsuite/inheritance.py @@ -159,6 +159,29 @@ class InheritanceTestCase(JinjaTestCase): '{{ super() }}|{{ item * 2 }}{% endblock %}') assert t.render(seq=range(5)) == '[0|0][1|2][2|4][3|6][4|8]' + def test_scoped_block_after_inheritance(self): + env = Environment(loader=DictLoader({ + 'layout.html': ''' + {% block useless %}{% endblock %} + ''', + 'index.html': ''' + {%- extends 'layout.html' %} + {% from 'helpers.html' import foo with context %} + {% block useless %} + {% for x in [1, 2, 3] %} + {% block testing scoped %} + {{ foo(x) }} + {% endblock %} + {% endfor %} + {% endblock %} + ''', + 'helpers.html': ''' + {% macro foo(x) %}{{ the_foo + x }}{% endmacro %} + ''' + })) + rv = env.get_template('index.html').render(the_foo=42).split() + assert rv == ['43', '44', '45'] + class BugFixTestCase(JinjaTestCase): -- 2.26.2