Skip to content
Myr

Implement with-tag with a custom node

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 75bbd40 · Browse files · 7 files+56 added−11 removed
This commit

State

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

Diff stats

7 files+56 added−11 removed

  • tests:+10 added−2 removed
  • other:+46 added−9 removed

7 files+56 added−11 removed

modified:jinja2/compiler.py+13 added-1 removed
17 unmodified lines
18from jinja2.optimizer import Optimizer19from jinja2.exceptions import TemplateAssertionError20from jinja2.utils import Markup, concat, escape21from jinja2._compat import range_type, text_type, string_types, \22 iteritems, NativeStringIO, imap22 iteritems, NativeStringIO, imap, izip23from jinja2.idtracking import Symbols, VAR_LOAD_PARAMETER, \24 VAR_LOAD_RESOLVE, VAR_LOAD_ALIAS, VAR_LOAD_UNDEFINED2526
1137 unmodified lines
1164 self.visit_Filter(node.filter, filter_frame)1165 self.end_write(frame)1166 self.leave_frame(filter_frame)11671168 def visit_With(self, node, frame):1169 with_frame = frame.inner()1170 with_frame.symbols.analyze_node(node)1171 self.enter_frame(with_frame)1172 for idx, (target, expr) in enumerate(izip(node.targets, node.values)):1173 self.newline()1174 self.visit(target, with_frame)1175 self.write(' = ')1176 self.visit(expr, frame)1177 self.blockvisit(node.body, with_frame)1178 self.leave_frame(with_frame)11791180 def visit_ExprStmt(self, node, frame):1181 self.newline(node)1182 self.visit(node.node, frame)1183
470 unmodified lines
modified:jinja2/idtracking.py+10 added
178 unmodified lines
179 raise RuntimeError('Unknown for branch')180 for item in branch or ():181 self.sym_visitor.visit(item)182183 def visit_With(self, node, **kwargs):184 for target in node.targets:185 self.sym_visitor.visit(target)186 for child in node.body:187 self.sym_visitor.visit(child)188189 def generic_visit(self, node, *args, **kwargs):190 raise NotImplementedError('Cannot find symbols for %r' %191 node.__class__.__name__)192
61 unmodified lines
254255 def visit_FilterBlock(self, node, **kwargs):256 self.visit(node.filter, **kwargs)257258 def visit_With(self, node, **kwargs):259 for target in node.values:260 self.visit(target)261262 def visit_AssignBlock(self, node, **kwargs):263 """Stop visiting at block assigns."""264 self.visit(node.target, **kwargs)265
5 unmodified lines
modified:jinja2/nodes.py+9 added
335 unmodified lines
336 """Node for filter sections."""337 fields = ('body', 'filter')338339340class With(Stmt):341 """Specific node for with statements. In older versions of Jinja the342 with statement was implemented on the base of the `Scope` node instead.343344 .. versionadded:: 2.9.3345 """346 fields = ('targets', 'values', 'body')347348349class Block(Stmt):350 """A node that represents a block."""351 fields = ('name', 'body', 'scoped')352
587 unmodified lines
modified:jinja2/parser.py+11 added-8 removed
223 unmodified lines
224 break225 return result226227 def parse_with(self):228 node = nodes.Scope(lineno=next(self.stream).lineno)229 assignments = []228 node = nodes.With(lineno=next(self.stream).lineno)229 targets = []230 values = []231 while self.stream.current.type != 'block_end':232 lineno = self.stream.current.lineno232 if assignments:233 if targets:234 self.stream.expect('comma')235 target = self.parse_assign_target()236 target.set_ctx('param')237 targets.append(target)238 self.stream.expect('assign')236 expr = self.parse_expression()237 assignments.append(nodes.Assign(target, expr, lineno=lineno))238 node.body = assignments + \239 list(self.parse_statements(('name:endwith',),240 drop_needle=True))239 values.append(self.parse_expression())240 node.targets = targets241 node.values = values242 node.body = self.parse_statements(('name:endwith',),243 drop_needle=True)244 return node245246 def parse_autoescape(self):247 node = nodes.ScopedEvalContextModifier(lineno=next(self.stream).lineno)
651 unmodified lines
modified:tests/test_core_tags.py+8 added
376 unmodified lines
377 {{ a }} = {{ b }}\378 ''')379 assert [x.strip() for x in tmpl.render(a=1, b=2).splitlines()] \380 == ['42 = 23', '1 = 2']381382 def test_with_argument_scoping(self, env):383 tmpl = env.from_string('''\384 {%- with a=1, b=2, c=b, d=e, e=5 -%}385 {{ a }}|{{ b }}|{{ c }}|{{ d }}|{{ e }}386 {%- endwith -%}387 ''')388 assert tmpl.render(b=3, e=4) == '1|2|3|4|5'
modified:tests/test_ext.py+2 added-2 removed
327 unmodified lines
328 return node329330 env = Environment(extensions=[ScopeExt])331 tmpl = env.from_string('''\332 {%- with a=1, b=2, c=b, d=e, e=5 -%}332 {%- scope a=1, b=2, c=b, d=e, e=5 -%}333 {{ a }}|{{ b }}|{{ c }}|{{ d }}|{{ e }}334 {%- endwith -%}334 {%- endscope -%}335 ''')336 assert tmpl.render(b=3, e=4) == '1|2|2|4|5'337338
159 unmodified lines
modified:CHANGES+3 added
14 unmodified lines
15 For more information and a discussion see #64116- Resolved an issue where `block scoped` would not take advantage of the17 new scoping rules. In some more exotic cases a variable overriden in a18 local scope would not make it into a block.19- Change the code generation of the `with` statement to be in line with the20 new scoping rules. This resolves some unlikely bugs in edge cases. This21 also introduces a new internal `With` node that can be used by extensions.2223Version 2.9.224-------------25(bugfix release, released on January 8th 2017)
447 unmodified lines