Skip to content
Myr

Made it possible to refer to names from outer scopes in included templates

Before adoption: this commit came in with the history the project adopted, so no landing records it.

that were unused in the callers frame (#327). --HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 7850dc5 · Browse files · 3 files+31 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+31 added−1 removed

  • tests:+15 added−1 removed
  • other:+16 added−0 removed

3 files+31 added−1 removed

modified:jinja2/compiler.py+14 added
518 unmodified lines
519 mapping[name] = self.temporary_identifier()520 self.writeline('%s = environment.%s[%r]' %521 (mapping[name], dependency, name))522523 def unoptimize_scope(self, frame):524 """Disable Python optimizations for the frame."""525 # XXX: this is not that nice but it has no real overhead. It526 # mainly works because python finds the locals before dead code527 # is removed. If that breaks we have to add a dummy function528 # that just accepts the arguments and does nothing.529 if frame.identifiers.declared:530 self.writeline('if 0: dummy(%s)' % ', '.join(531 'l_' + name for name in frame.identifiers.declared))532533 def push_scope(self, frame, extra_vars=()):534 """This function returns all the shadowed variables in a dict535 in the form name: alias and will write the required assignments536 into the current scope. No indentation takes place.
293 unmodified lines
830 self.extends_so_far += 1831832 def visit_Include(self, node, frame):833 """Handles includes."""834 if node.with_context:835 self.unoptimize_scope(frame)836 if node.ignore_missing:837 self.writeline('try:')838 self.indent()839 self.writeline('template = environment.get_template(', node)
23 unmodified lines
863 self.outdent()864865 def visit_Import(self, node, frame):866 """Visit regular imports."""867 if node.with_context:868 self.unoptimize_scope(frame)869 self.writeline('l_%s = ' % node.target, node)870 if frame.toplevel:871 self.write('context.vars[%r] = ' % node.target)872 self.write('environment.get_template(')
602 unmodified lines
modified:tests/test_imports.py+15 added-1 removed
11 unmodified lines
121314test_env = Environment(loader=DictLoader(dict(15 module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}',16 header='[{{ foo }}|{{ 23 }}]'16 header='[{{ foo }}|{{ 23 }}]',17 o_printer='({{ o }})'18)))19test_env.globals['bar'] = 232021
58 unmodified lines
80 assert m.toplevel() == '...'81 assert not hasattr(m, '__missing')82 assert m.variable == 4283 assert not hasattr(m, 'notthere')848586def test_unoptimized_scopes():87 t = test_env.from_string("""88 {% macro outer(o) %}89 {% macro inner() %}90 {% include "o_printer" %}91 {% endmacro %}92 {{ inner() }}93 {% endmacro %}94 {{ outer("FOO") }}95 """)96 assert t.render().strip() == '(FOO)'
modified:CHANGES+2 added
11 unmodified lines
12 required parentheses (`not (foo in bar)`) which was odd.13- Fixed a bug that caused syntax errors when defining macros or using the14 `{% call %}` tag inside loops.15- Fixed a bug in the parser that made ``{{ foo[1, 2] }}`` impossible.16- Made it possible to refer to names from outer scopes in included templates17 that were unused in the callers frame (#327)1819Version 2.1.120-------------21(Bugfix release)
84 unmodified lines