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
Provenance
- Commit
9b4cc9f- Tree
160032a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
8e64adf
5 files changed+81 added−0 removed
modified:docs/extensions.rst+14 added
110 unmodified lines111 This extension adds support for `break` and `continue` in loops. After112 enabling Jinja2 provides those two keywords which work exactly like in113 Python.114 115+.. _with-extension:116 117+With Statement118+--------------119+120+**Import name:** `jinja2.ext.with_`121+122+.. versionadded:: 2.3123+124+This extension adds support for the with keyword. Using this keyword it125+is possible to enforce a nested scope in a template. Variables can be126+declared directly in the opening block of the with statement or using a127+standard `set` statement directly within.128+129+130 .. _writing-extensions:131 132 Writing Extensions133 ------------------135 unmodified linesmodified:docs/templates.rst+32 added
1250 unmodified lines1251 1252 {% for user in users %}1253 {%- if loop.index >= 10 %}{% break %}{% endif %}1254 {%- endfor %}1255+1256+1257+With Statement1258+~~~~~~~~~~~~~~1259+1260+.. versionadded:: 2.31261+1262+If the application enables the :ref:`with-extension` it is possible to1263+use the `with` keyword in templates. This makes it possible to create1264+a new inner scope. Variables set within this scope are not visible1265+outside of the scope.1266+1267+With in a nutshell::1268+1269+ {% with %}1270+ {% set foo = 42 %}1271+ {{ foo }} foo is 42 here1272+ {% endwith %}1273+ foo is not visible here any longer1274+1275+Because it is common to set variables at the beginning of the scope1276+you can do that within the with statement. The following two examples1277+are equivalent::1278+1279+ {% with foo = 42 %}1280+ {{ foo }}1281+ {% endwith %}1282+1283+ {% with %}1284+ {% set foo = 42 %}1285+ {{ foo }}1286+ {% endwith %}modified:jinja2/ext.py+22 added
334 unmodified lines335 return nodes.Break(lineno=token.lineno)336 return nodes.Continue(lineno=token.lineno)337 338 339+class WithExtension(Extension):340+ """Adds support for a django-like with block."""341+ tags = set(['with'])342+343+ 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 node358+359+360 def 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 string163 unmodified lines527 #: nicer import names528 i18n = InternationalizationExtension529 do = ExprStmtExtension530 loopcontrols = LoopControlExtension531+with_ = WithExtensionmodified:tests/test_ext.py+12 added
104 unmodified lines105 {%- endfor %}{{ items|join(', ') }}''')106 assert tmpl.render() == '0f, 1o, 2o'107 108 109+def 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']119+120+121 def test_extension_nodes():122 env = Environment(extensions=[TestExtension])123 tmpl = env.from_string('{% test %}')124 assert tmpl.render() == 'False|42|23|{}'49 unmodified linesmodified:CHANGES+1 added
17 unmodified lines18 - 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.23 24 Version 2.2.125 -------------26 (bugfix release, released on September 14th 2009)114 unmodified lines