mitsuhiko / jinja2-main (http://jinja.pocoo.org/)

Mirror of jinja2-main.

Clone this repository (size: 3.7 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/mitsuhiko/jinja2-main/

Changed (Δ2.2 KB):

raw changeset »

CHANGES (1 lines added, 0 lines removed)

docs/extensions.rst (14 lines added, 0 lines removed)

docs/templates.rst (32 lines added, 0 lines removed)

jinja2/ext.py (22 lines added, 0 lines removed)

tests/test_ext.py (12 lines added, 0 lines removed)

Up to file-list CHANGES:

@@ -19,6 +19,7 @@ Version 2.3
19
19
  This change makes ``{% if %}...{% endif %}`` a syntax error
20
20
  now. (#364)
21
21
- added support for translator comments if extracted via babel.
22
- added with-statement extension.
22
23
23
24
Version 2.2.1
24
25
-------------

Up to file-list docs/extensions.rst:

@@ -112,6 +112,20 @@ This extension adds support for `break`
112
112
enabling Jinja2 provides those two keywords which work exactly like in
113
113
Python.
114
114
115
.. _with-extension:
116
117
With Statement
118
--------------
119
120
**Import name:** `jinja2.ext.with_`
121
122
.. versionadded:: 2.3
123
124
This extension adds support for the with keyword.  Using this keyword it
125
is possible to enforce a nested scope in a template.  Variables can be
126
declared directly in the opening block of the with statement or using a
127
standard `set` statement directly within.
128
115
129
116
130
.. _writing-extensions:
117
131

Up to file-list docs/templates.rst:

@@ -1252,3 +1252,35 @@ Likewise a look that stops processing af
1252
1252
    {% for user in users %}
1253
1253
        {%- if loop.index >= 10 %}{% break %}{% endif %}
1254
1254
    {%- endfor %}
1255
1256
1257
With Statement
1258
~~~~~~~~~~~~~~
1259
1260
.. versionadded:: 2.3
1261
1262
If the application enables the :ref:`with-extension` it is possible to
1263
use the `with` keyword in templates.  This makes it possible to create
1264
a new inner scope.  Variables set within this scope are not visible
1265
outside of the scope.
1266
1267
With in a nutshell::
1268
1269
    {% with %}
1270
        {% set foo = 42 %}
1271
        {{ foo }}           foo is 42 here
1272
    {% endwith %}
1273
    foo is not visible here any longer
1274
1275
Because it is common to set variables at the beginning of the scope
1276
you can do that within the with statement.  The following two examples
1277
are equivalent::
1278
1279
    {% with foo = 42 %}
1280
        {{ foo }}
1281
    {% endwith %}
1282
1283
    {% with %}
1284
        {% set foo = 42 %}
1285
        {{ foo }}
1286
    {% endwith %}

Up to file-list jinja2/ext.py:

@@ -336,6 +336,27 @@ class LoopControlExtension(Extension):
336
336
        return nodes.Continue(lineno=token.lineno)
337
337
338
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.lineno
348
            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 node
358
359
339
360
def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS,
340
361
                     babel_style=True):
341
362
    """Extract localizable strings from the given template node.  Per
@@ -507,3 +528,4 @@ def babel_extract(fileobj, keywords, com
507
528
i18n = InternationalizationExtension
508
529
do = ExprStmtExtension
509
530
loopcontrols = LoopControlExtension
531
with_ = WithExtension

Up to file-list tests/test_ext.py:

@@ -106,6 +106,18 @@ def test_do():
106
106
    assert tmpl.render() == '0f, 1o, 2o'
107
107
108
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
109
121
def test_extension_nodes():
110
122
    env = Environment(extensions=[TestExtension])
111
123
    tmpl = env.from_string('{% test %}')