Skip to content
Myr

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

6 files+49 added−3 removed

modified:docs/templates.rst+11 added
945 unmodified lines
946 {% endset %}947948The `navigation` variable then contains the navigation HTML source.949950.. versionchanged:: 2.10951952Starting with Jinja 2.10, the block assignment supports filters.953954Example::955956 {% set reply | wordwrap %}957 You wrote:958 {{ message }}959 {% endset %}960961962.. _extends:963964Extends965~~~~~~~
704 unmodified lines
modified:jinja2/compiler.py+6 added-1 removed
1383 unmodified lines
1384 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)13961397 # -- Expression Visitors
324 unmodified lines
modified:jinja2/nodes.py+1 added-1 removed
385 unmodified lines
386387388class AssignBlock(Stmt):389 """Assigns a block to a target."""390 fields = ('target', 'body')390 fields = ('target', 'filter', 'body')391392393class Expr(Node):394 """Baseclass for all expressions."""
605 unmodified lines
modified:jinja2/parser.py+2 added-1 removed
178 unmodified lines
179 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)187188 def parse_for(self):189 """Parse a for loop."""190 lineno = self.stream.expect('name:for').lineno
713 unmodified lines
modified:tests/test_core_tags.py+27 added
451 unmodified lines
452 '{{ magic(ns) }}'453 '{{ ns.a }}|{{ ns.b }}')454 assert tmpl.render() == '13|37'455456 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>&lt;unsafe&gt;</em>'461462 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'468469 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'482483484@pytest.mark.core_tags485@pytest.mark.with_486class TestWith(object):487
17 unmodified lines
modified:CHANGES+2 added
31 unmodified lines
32- ``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`_)3738.. _#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/685
525 unmodified lines