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
Provenance
- Commit
06238a9- Tree
f2b56b2- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
41df8a5- Parent 2
1539dd4
4 files changed+36 added−4 removed
modified:jinja2/compiler.py+3 added
1718 unmodified lines1719 1720 def visit_ContextReference(self, node, frame):1721 self.write('context')1722 1723+ def visit_DerivedContextReference(self, node, frame):1724+ self.write(self.derive_context(frame))1725+1726 def visit_Continue(self, node, frame):1727 self.writeline('continue', node)1728 1729 def visit_Break(self, node, frame):43 unmodified linesmodified:jinja2/nodes.py+9 added
950 unmodified lines951 as the first argument to a function.952 """953 954 955+class 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.959+960+ .. versionadded:: 2.11961+ """962+963+964 class Continue(Stmt):965 """Continue a loop."""966 967 48 unmodified linesmodified:tests/test_ext.py+21 added-4 removed
107 unmodified lines108 109 class ExampleExtension(Extension):110 tags = set(['test'])111 ext_attr = 42112+ context_reference_node_cls = nodes.ContextReference113 114 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)121 122 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 )130 131 132+class DerivedExampleExtension(ExampleExtension):133+ context_reference_node_cls = nodes.DerivedContextReference134+135+136 class PreprocessorExtension(Extension):137 138 def preprocess(self, source, name, filename=None):139 return source.replace('[[TEST]]', '({{ foo }})')70 unmodified lines210 211 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'215 216+ 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'220+221+ 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'226+227 def test_identifier(self):228 assert ExampleExtension.identifier == __name__ + '.ExampleExtension'229 230 def test_rebinding(self):383 unmodified linesmodified:CHANGES.rst+3 added
83 unmodified lines84 - 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`91 92 93 Version 2.10.394 --------------629 unmodified lines