Skip to content
Myr

added with-statement extension.

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

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 8e64adf · Browse files · 5 files+81 added−0 removed
This commit

State

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

Diff stats

5 files+81 added−0 removed

  • tests:+12 added−0 removed
  • other:+69 added−0 removed

5 files+81 added−0 removed

modified:docs/extensions.rst+14 added
110 unmodified lines
111This extension adds support for `break` and `continue` in loops. After112enabling Jinja2 provides those two keywords which work exactly like in113Python.114115.. _with-extension:116117With Statement118--------------119120**Import name:** `jinja2.ext.with_`121122.. versionadded:: 2.3123124This extension adds support for the with keyword. Using this keyword it125is possible to enforce a nested scope in a template. Variables can be126declared directly in the opening block of the with statement or using a127standard `set` statement directly within.128129130.. _writing-extensions:131132Writing Extensions133------------------
135 unmodified lines
modified:docs/templates.rst+32 added
1250 unmodified lines
12511252 {% for user in users %}1253 {%- if loop.index >= 10 %}{% break %}{% endif %}1254 {%- endfor %}125512561257With Statement1258~~~~~~~~~~~~~~12591260.. versionadded:: 2.312611262If the application enables the :ref:`with-extension` it is possible to1263use the `with` keyword in templates. This makes it possible to create1264a new inner scope. Variables set within this scope are not visible1265outside of the scope.12661267With in a nutshell::12681269 {% with %}1270 {% set foo = 42 %}1271 {{ foo }} foo is 42 here1272 {% endwith %}1273 foo is not visible here any longer12741275Because it is common to set variables at the beginning of the scope1276you can do that within the with statement. The following two examples1277are equivalent::12781279 {% with foo = 42 %}1280 {{ foo }}1281 {% endwith %}12821283 {% with %}1284 {% set foo = 42 %}1285 {{ foo }}1286 {% endwith %}
modified:jinja2/ext.py+22 added
334 unmodified lines
335 return nodes.Break(lineno=token.lineno)336 return nodes.Continue(lineno=token.lineno)337338339class WithExtension(Extension):340 """Adds support for a django-like with block."""341 tags = set(['with'])342343 def parse(self, parser):344 node = nodes.Scope(lineno=next(parser.stream).lineno)345 assignments = []346 while parser.stream.current.type != 'block_end':347 lineno = parser.stream.current.lineno348 if assignments:349 parser.stream.expect('comma')350 target = parser.parse_assign_target()351 parser.stream.expect('assign')352 expr = parser.parse_expression()353 assignments.append(nodes.Assign(target, expr, lineno=lineno))354 node.body = assignments + \355 list(parser.parse_statements(('name:endwith',),356 drop_needle=True))357 return node358359360def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS,361 babel_style=True):362 """Extract localizable strings from the given template node. Per363 default this function returns matches in babel style that means non string
163 unmodified lines
527#: nicer import names528i18n = InternationalizationExtension529do = ExprStmtExtension530loopcontrols = LoopControlExtension531with_ = WithExtension
modified:tests/test_ext.py+12 added
104 unmodified lines
105 {%- endfor %}{{ items|join(', ') }}''')106 assert tmpl.render() == '0f, 1o, 2o'107108109def test_with():110 env = Environment(extensions=['jinja2.ext.with_'])111 tmpl = env.from_string('''\112 {% with a=42, b=23 -%}113 {{ a }} = {{ b }}114 {% endwith -%}115 {{ a }} = {{ b }}\116 ''')117 assert [x.strip() for x in tmpl.render(a=1, b=2).splitlines()] \118 == ['42 = 23', '1 = 2']119120121def test_extension_nodes():122 env = Environment(extensions=[TestExtension])123 tmpl = env.from_string('{% test %}')124 assert tmpl.render() == 'False|42|23|{}'
49 unmodified lines
modified:CHANGES+1 added
17 unmodified lines
18- implicit tuple expressions can no longer be totally empty.19 This change makes ``{% if %}...{% endif %}`` a syntax error20 now. (#364)21- added support for translator comments if extracted via babel.22- added with-statement extension.2324Version 2.2.125-------------26(bugfix release, released on September 14th 2009)
114 unmodified lines