Skip to content
Myr

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

6 files+57 added−54 removed

modified:docs/api.rst+8 added-7 removed
245 unmodified lines
246247Autoescaping248------------249250.. versionadded:: 2.4250.. versionchanged:: 2.4251252As of Jinja 2.4 the preferred way to do autoescaping is to enable the253:ref:`autoescape-extension` and to configure a sensible default for254autoescaping. This makes it possible to enable and disable autoescaping255on a per-template basis (HTML versus text for instance).252Jinja2 now comes with autoescaping support. As of Jinja 2.9 the253autoescape extension is removed and built-in. However autoescaping is254not yet enabled by default though this might change in the future.255It's recommended to configure a sensible default for autoescaping. This256makes it possible to enable and disable autoescaping on a per-template257basis (HTML versus text for instance).258259Here a recommended setup that enables autoescaping for templates ending260in ``'.html'``, ``'.htm'`` and ``'.xml'`` and disabling it by default261for all other extensions::
4 unmodified lines
266 ext = template_name.rsplit('.', 1)[1]267 return ext in ('html', 'htm', 'xml')268269 env = Environment(autoescape=guess_autoescape,268 loader=PackageLoader('mypackage'),269 extensions=['jinja2.ext.autoescape'])270 loader=PackageLoader('mypackage'))271272When implementing a guessing autoescape function, make sure you also273accept `None` as valid template name. This will be passed when generating274templates from strings.
626 unmodified lines
modified:docs/extensions.rst+5 added-10 removed
181 unmodified lines
182--------------183184**Import name:** `jinja2.ext.with_`185186.. versionadded:: 2.3186.. versionchanged:: 2.9187188This extension adds support for the with keyword. Using this keyword it189is possible to enforce a nested scope in a template. Variables can be190declared directly in the opening block of the with statement or using a191standard `set` statement directly within.188This extension is now built-in and no longer does anything.189190.. _autoescape-extension:191192Autoescape Extension193--------------------194195**Import name:** `jinja2.ext.autoescape`196200.. versionadded:: 2.4197.. versionchanged:: 2.9198202The autoescape extension allows you to toggle the autoescape feature from203within the template. If the environment's :attr:`~Environment.autoescape`204setting is set to `False` it can be activated, if it's `True` it can be205deactivated. The setting overriding is scoped.199This extension was removed and is now built-in. Enabling the extension200no longer does anything.201202203.. _writing-extensions:204
137 unmodified lines
modified:docs/templates.rst+15 added-7 removed
1458 unmodified lines
1459~~~~~~~~~~~~~~14601461.. versionadded:: 2.314621463If the application enables the :ref:`with-extension`, it is possible to1464use the `with` keyword in templates. This makes it possible to create1465a new inner scope. Variables set within this scope are not visible1466outside of the scope.1463The with statement makes it possible to create a new inner scope.1464Variables set within this scope are not visible outside of the scope.14651466With in a nutshell::14671468 {% with %}
14 unmodified lines
1483 {% set foo = 42 %}1484 {{ foo }}1485 {% endwith %}14861487.. admonition:: Extension14881489 In older versions of Jinja (before 2.9) it was required to enable this1490 feature with an extension. It's now enabled by default.14911492.. _autoescape-overrides:14931491Autoescape Extension1494Autoescape Overrides1495--------------------14961497.. versionadded:: 2.414981496If the application enables the :ref:`autoescape-extension`, one can1497activate and deactivate the autoescaping from within the templates.1499If you want you can activate and deactivate the autoescaping from within1500the templates.15011502Example::15031504 {% autoescape true %}
4 unmodified lines
1509 Autoescaping is inactive within this block1510 {% endautoescape %}15111512After an `endautoescape` the behavior is reverted to what it was before.15131514.. admonition:: Extension15151516 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 lines
410 return nodes.Continue(lineno=token.lineno)411412413class WithExtension(Extension):414 """Adds support for a django-like with block."""415 tags = set(['with'])414 pass415417 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 node416433417class AutoEscapeExtension(Extension):435 """Changes auto escape rules for a scope."""436 tags = set(['autoescape'])418 pass419438 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])420447421def 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 string
185 unmodified lines
modified:jinja2/parser.py+26 added-1 removed
14 unmodified lines
151617_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'])2122_math_nodes = {23 'add': nodes.Add,
199 unmodified lines
223 node.else_ = []224 break225 return result226227 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 node242243 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])251252 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 lines
modified:CHANGES+1 added
38 unmodified lines
39 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.4445Version 2.8.246-------------47
381 unmodified lines