Support filters in set block
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- e.g {% set foo | trim %}...{% endset %} - closes #486
authored by ThiefMaster · committed by Adrian Moennich · parent d17c7db · Browse files · 6 files·+49 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+49 added−3 removed
- tests:+27 added−0 removed
- other:+22 added−3 removed
Provenance
- Commit
072cdf9- Tree
fcd3f92- Author
ThiefMaster <adrian@planetcoding.net>- Committer
Adrian Moennich <adrian@planetcoding.net>- Parent
d17c7db
6 files changed+49 added−3 removed
modified:docs/templates.rst+11 added
945 unmodified lines946 {% endset %}947 948 The `navigation` variable then contains the navigation HTML source.949 950+.. versionchanged:: 2.10951 952+Starting with Jinja 2.10, the block assignment supports filters.953+954+Example::955+956+ {% set reply | wordwrap %}957+ You wrote:958+ {{ message }}959+ {% endset %}960+961+962 .. _extends:963 964 Extends965 ~~~~~~~704 unmodified linesmodified:jinja2/compiler.py+6 added-1 removed
1383 unmodified lines1384 self.blockvisit(node.body, block_frame)1385 self.newline(node)1386 self.visit(node.target, frame)1387 self.write(' = (Markup if context.eval_ctx.autoescape '1388- 'else identity)(concat(%s))' % block_frame.buffer)1388+ 'else identity)(')1389+ if node.filter is not None:1390+ self.visit_Filter(node.filter, block_frame)1391+ else:1392+ self.write('concat(%s)' % block_frame.buffer)1393+ self.write(')')1394 self.pop_assign_tracking(frame)1395 self.leave_frame(block_frame)1396 1397 # -- Expression Visitors324 unmodified linesmodified:jinja2/nodes.py+1 added-1 removed
385 unmodified lines386 387 388 class AssignBlock(Stmt):389 """Assigns a block to a target."""390- fields = ('target', 'body')390+ fields = ('target', 'filter', 'body')391 392 393 class Expr(Node):394 """Baseclass for all expressions."""605 unmodified linesmodified:jinja2/parser.py+2 added-1 removed
178 unmodified lines179 target = self.parse_assign_target(with_namespace=True)180 if self.stream.skip_if('assign'):181 expr = self.parse_tuple()182 return nodes.Assign(target, expr, lineno=lineno)183+ filter_node = self.parse_filter(None)184 body = self.parse_statements(('name:endset',),185 drop_needle=True)185- return nodes.AssignBlock(target, body, lineno=lineno)186+ return nodes.AssignBlock(target, filter_node, body, lineno=lineno)187 188 def parse_for(self):189 """Parse a for loop."""190 lineno = self.stream.expect('name:for').lineno713 unmodified linesmodified:tests/test_core_tags.py+27 added
451 unmodified lines452 '{{ magic(ns) }}'453 '{{ ns.a }}|{{ ns.b }}')454 assert tmpl.render() == '13|37'455 456+ def test_block_escaping_filtered(self):457+ env = Environment(autoescape=True)458+ tmpl = env.from_string('{% set foo | trim %}<em>{{ test }}</em> '459+ '{% endset %}foo: {{ foo }}')460+ assert tmpl.render(test='<unsafe>') == 'foo: <em><unsafe></em>'461 462+ def test_block_filtered(self, env_trim):463+ tmpl = env_trim.from_string(464+ '{% set foo | trim | length | string %} 42 {% endset %}'465+ '{{ foo }}')466+ assert tmpl.render() == '2'467+ assert tmpl.module.foo == u'2'468+469+ def test_block_filtered_set(self, env_trim):470+ def _myfilter(val, arg):471+ assert arg == ' xxx '472+ return val473+ env_trim.filters['myfilter'] = _myfilter474+ tmpl = env_trim.from_string(475+ '{% set a = " xxx " %}'476+ '{% set foo | myfilter(a) | trim | length | string %}'477+ ' {% set b = " yy " %} 42 {{ a }}{{ b }} '478+ '{% endset %}'479+ '{{ foo }}')480+ assert tmpl.render() == '11'481+ assert tmpl.module.foo == u'11'482+483+484 @pytest.mark.core_tags485 @pytest.mark.with_486 class TestWith(object):487 17 unmodified linesmodified:CHANGES+2 added
31 unmodified lines32 - ``indent`` filter will not indent blank lines by default. (`#685`_)33 - Add ``reverse`` argument for ``dictsort`` filter. (`#692`_)34 - Add a ``NativeEnvironment`` that renders templates to native Python types35 instead of strings. (`#708`_)36+- Added filter support to the block ``set`` tag. (`#489`_)37 38 .. _#469: https://github.com/pallets/jinja/pull/46939 .. _#475: https://github.com/pallets/jinja/pull/47540 .. _#478: https://github.com/pallets/jinja/pull/47841+.. _#489: https://github.com/pallets/jinja/pull/48942 .. _#617: https://github.com/pallets/jinja/pull/61743 .. _#618: https://github.com/pallets/jinja/pull/61844 .. _#665: https://github.com/pallets/jinja/pull/66545 .. _#685: https://github.com/pallets/jinja/pull/685525 unmodified lines