Move the autoescape extension and with statement to be built-in
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent d67f0fd · Browse files · 6 files·+57 added−54 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+57 added−54 removed
- other:+57 added−54 removed
Provenance
- Commit
a27a503- Tree
949189f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d67f0fd
6 files changed+57 added−54 removed
modified:docs/api.rst+8 added-7 removed
245 unmodified lines246 247 Autoescaping248 ------------249 250-.. versionadded:: 2.4250+.. versionchanged:: 2.4251 252-As of Jinja 2.4 the preferred way to do autoescaping is to enable the253-:ref:`autoescape-extension` and to configure a sensible default for254-autoescaping. This makes it possible to enable and disable autoescaping255-on a per-template basis (HTML versus text for instance).252+Jinja2 now comes with autoescaping support. As of Jinja 2.9 the253+autoescape extension is removed and built-in. However autoescaping is254+not yet enabled by default though this might change in the future.255+It's recommended to configure a sensible default for autoescaping. This256+makes it possible to enable and disable autoescaping on a per-template257+basis (HTML versus text for instance).258 259 Here a recommended setup that enables autoescaping for templates ending260 in ``'.html'``, ``'.htm'`` and ``'.xml'`` and disabling it by default261 for all other extensions::4 unmodified lines266 ext = template_name.rsplit('.', 1)[1]267 return ext in ('html', 'htm', 'xml')268 269 env = Environment(autoescape=guess_autoescape,268- loader=PackageLoader('mypackage'),269- extensions=['jinja2.ext.autoescape'])270+ loader=PackageLoader('mypackage'))271 272 When implementing a guessing autoescape function, make sure you also273 accept `None` as valid template name. This will be passed when generating274 templates from strings.626 unmodified linesmodified:docs/extensions.rst+5 added-10 removed
181 unmodified lines182 --------------183 184 **Import name:** `jinja2.ext.with_`185 186-.. versionadded:: 2.3186+.. versionchanged:: 2.9187 188-This extension adds support for the with keyword. Using this keyword it189-is possible to enforce a nested scope in a template. Variables can be190-declared directly in the opening block of the with statement or using a191-standard `set` statement directly within.188+This extension is now built-in and no longer does anything.189 190 .. _autoescape-extension:191 192 Autoescape Extension193 --------------------194 195 **Import name:** `jinja2.ext.autoescape`196 200-.. versionadded:: 2.4197+.. versionchanged:: 2.9198 202-The autoescape extension allows you to toggle the autoescape feature from203-within the template. If the environment's :attr:`~Environment.autoescape`204-setting is set to `False` it can be activated, if it's `True` it can be205-deactivated. The setting overriding is scoped.199+This extension was removed and is now built-in. Enabling the extension200+no longer does anything.201 202 203 .. _writing-extensions:204 137 unmodified linesmodified:docs/templates.rst+15 added-7 removed
1458 unmodified lines1459 ~~~~~~~~~~~~~~1460 1461 .. versionadded:: 2.31462 1463-If the application enables the :ref:`with-extension`, it is possible to1464-use the `with` keyword in templates. This makes it possible to create1465-a new inner scope. Variables set within this scope are not visible1466-outside of the scope.1463+The with statement makes it possible to create a new inner scope.1464+Variables set within this scope are not visible outside of the scope.1465 1466 With in a nutshell::1467 1468 {% with %}14 unmodified lines1483 {% set foo = 42 %}1484 {{ foo }}1485 {% endwith %}1486 1487+.. admonition:: Extension1488+1489+ In older versions of Jinja (before 2.9) it was required to enable this1490+ feature with an extension. It's now enabled by default.1491+1492 .. _autoescape-overrides:1493 1491-Autoescape Extension1494+Autoescape Overrides1495 --------------------1496 1497 .. versionadded:: 2.41498 1496-If the application enables the :ref:`autoescape-extension`, one can1497-activate and deactivate the autoescaping from within the templates.1499+If you want you can activate and deactivate the autoescaping from within1500+the templates.1501 1502 Example::1503 1504 {% autoescape true %}4 unmodified lines1509 Autoescaping is inactive within this block1510 {% endautoescape %}1511 1512 After an `endautoescape` the behavior is reverted to what it was before.1513+1514+.. admonition:: Extension1515+1516+ In older versions of Jinja (before 2.9) it was required to enable this1517+ feature with an extension. It's now enabled by default.modified:jinja2/ext.py+2 added-29 removed
409 unmodified lines410 return nodes.Continue(lineno=token.lineno)411 412 413 class WithExtension(Extension):414- """Adds support for a django-like with block."""415- tags = set(['with'])414+ pass415 417- def parse(self, parser):418- node = nodes.Scope(lineno=next(parser.stream).lineno)419- assignments = []420- while parser.stream.current.type != 'block_end':421- lineno = parser.stream.current.lineno422- if assignments:423- parser.stream.expect('comma')424- target = parser.parse_assign_target()425- parser.stream.expect('assign')426- expr = parser.parse_expression()427- assignments.append(nodes.Assign(target, expr, lineno=lineno))428- node.body = assignments + \429- list(parser.parse_statements(('name:endwith',),430- drop_needle=True))431- return node416 433-417 class AutoEscapeExtension(Extension):435- """Changes auto escape rules for a scope."""436- tags = set(['autoescape'])418+ pass419 438- def parse(self, parser):439- node = nodes.ScopedEvalContextModifier(lineno=next(parser.stream).lineno)440- node.options = [441- nodes.Keyword('autoescape', parser.parse_expression())442- ]443- node.body = parser.parse_statements(('name:endautoescape',),444- drop_needle=True)445- return nodes.Scope([node])420 447-421 def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS,422 babel_style=True):423 """Extract localizable strings from the given template node. Per424 default this function returns matches in babel style that means non string185 unmodified linesmodified:jinja2/parser.py+26 added-1 removed
14 unmodified lines15 16 17 _statement_keywords = frozenset(['for', 'if', 'block', 'extends', 'print',18 'macro', 'include', 'from', 'import',19- 'set'])19+ 'set', 'with', 'autoescape'])20 _compare_operators = frozenset(['eq', 'ne', 'lt', 'lteq', 'gt', 'gteq'])21 22 _math_nodes = {23 'add': nodes.Add,199 unmodified lines223 node.else_ = []224 break225 return result226 227+ def parse_with(self):228+ node = nodes.Scope(lineno=next(self.stream).lineno)229+ assignments = []230+ while self.stream.current.type != 'block_end':231+ lineno = self.stream.current.lineno232+ if assignments:233+ self.stream.expect('comma')234+ target = self.parse_assign_target()235+ self.stream.expect('assign')236+ expr = self.parse_expression()237+ assignments.append(nodes.Assign(target, expr, lineno=lineno))238+ node.body = assignments + \239+ list(self.parse_statements(('name:endwith',),240+ drop_needle=True))241+ return node242+243+ def parse_autoescape(self):244+ node = nodes.ScopedEvalContextModifier(lineno=next(self.stream).lineno)245+ node.options = [246+ nodes.Keyword('autoescape', self.parse_expression())247+ ]248+ node.body = self.parse_statements(('name:endautoescape',),249+ drop_needle=True)250+ return nodes.Scope([node])251+252 def parse_block(self):253 node = nodes.Block(lineno=next(self.stream).lineno)254 node.name = self.stream.expect('name').value255 node.scoped = self.stream.skip_if('name:scoped')640 unmodified linesmodified:CHANGES+1 added
38 unmodified lines39 designers and developers expect. There is now no more magic difference40 between the different include and import constructs. Context is now always41 propagated the same way. The only remaining differences is the defaults42 for `with context` and `without context`.43+- The `with` and `autoescape` tags are now built-in.44 45 Version 2.8.246 -------------47 381 unmodified lines