Skip to content
Myr

Merge pull request #861 from OddBloke/contextreference_locals

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

Add a DerivedContextReference node type

authored by David Lord · committed by GitHub · parents 41df8a5, 1539dd4 · Browse files · 4 files+36 added−4 removed
This commit

State

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

Diff stats

4 files+36 added−4 removed

  • tests:+21 added−4 removed
  • other:+15 added−0 removed

4 files+36 added−4 removed

modified:jinja2/compiler.py+3 added
1718 unmodified lines
17191720 def visit_ContextReference(self, node, frame):1721 self.write('context')17221723 def visit_DerivedContextReference(self, node, frame):1724 self.write(self.derive_context(frame))17251726 def visit_Continue(self, node, frame):1727 self.writeline('continue', node)17281729 def visit_Break(self, node, frame):
43 unmodified lines
modified:jinja2/nodes.py+9 added
950 unmodified lines
951 as the first argument to a function.952 """953954955class DerivedContextReference(Expr):956 """Return the current template context including locals. Behaves957 exactly like :class:`ContextReference`, but includes local958 variables, such as from a ``for`` loop.959960 .. versionadded:: 2.11961 """962963964class Continue(Stmt):965 """Continue a loop."""966967
48 unmodified lines
modified:tests/test_ext.py+21 added-4 removed
107 unmodified lines
108109class ExampleExtension(Extension):110 tags = set(['test'])111 ext_attr = 42112 context_reference_node_cls = nodes.ContextReference113114 def parse(self, parser):115 return nodes.Output([self.call_method('_dump', [116 nodes.EnvironmentAttribute('sandboxed'),117 self.attr('ext_attr'),118 nodes.ImportedName(__name__ + '.importable_object'),118 nodes.ContextReference()119 self.context_reference_node_cls()120 ])]).set_lineno(next(parser.stream).lineno)121122 def _dump(self, sandboxed, ext_attr, imported_object, context):122 return '%s|%s|%s|%s' % (123 return '%s|%s|%s|%s|%s' % (124 sandboxed,125 ext_attr,126 imported_object,126 context.blocks127 context.blocks,128 context.get('test_var')129 )130131132class DerivedExampleExtension(ExampleExtension):133 context_reference_node_cls = nodes.DerivedContextReference134135136class PreprocessorExtension(Extension):137138 def preprocess(self, source, name, filename=None):139 return source.replace('[[TEST]]', '({{ foo }})')
70 unmodified lines
210211 def test_extension_nodes(self):212 env = Environment(extensions=[ExampleExtension])213 tmpl = env.from_string('{% test %}')208 assert tmpl.render() == 'False|42|23|{}'214 assert tmpl.render() == 'False|42|23|{}|None'215216 def test_contextreference_node_passes_context(self):217 env = Environment(extensions=[ExampleExtension])218 tmpl = env.from_string('{% set test_var="test_content" %}{% test %}')219 assert tmpl.render() == 'False|42|23|{}|test_content'220221 def test_contextreference_node_can_pass_locals(self):222 env = Environment(extensions=[DerivedExampleExtension])223 tmpl = env.from_string(224 '{% for test_var in ["test_content"] %}{% test %}{% endfor %}')225 assert tmpl.render() == 'False|42|23|{}|test_content'226227 def test_identifier(self):228 assert ExampleExtension.identifier == __name__ + '.ExampleExtension'229230 def test_rebinding(self):
383 unmodified lines
modified:CHANGES.rst+3 added
83 unmodified lines
84- Tracebacks for exceptions in templates show the correct line numbers85 and source for Python >= 3.7. :issue:`1104`86- Tracebacks for template syntax errors in Python 3 no longer show87 internal compiler frames. :issue:`763`88- Add a ``DerivedContextReference`` node that can be used by89 extensions to get the current context and local variables such as90 ``loop``. :issue:`860`919293Version 2.10.394--------------
629 unmodified lines