Updated docs on autoescaping and made select_autoescape case insensitive
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 0020a47 · Browse files · 3 files·+42 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+42 added−18 removed
- tests:+2 added−0 removed
- other:+40 added−18 removed
Provenance
- Commit
b81a8a3- Tree
961eaf7- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
0020a47
3 files changed+42 added−18 removed
modified:docs/api.rst+33 added-16 removed
24 unmodified lines25 26 The simplest way to configure Jinja2 to load templates for your application27 looks roughly like this::28 29- from jinja2 import Environment, PackageLoader30- env = Environment(loader=PackageLoader('yourapplication', 'templates'))29+ from jinja2 import Environment, PackageLoader, select_autoescape30+ env = Environment(31+ loader=PackageLoader('yourapplication', 'templates'),32+ autoescape=select_autoescape(['html', 'xml'])33+ )34 35 This will create a template environment with the default settings and a36 loader that looks up the templates in the `templates` folder inside the37 `yourapplication` python package. Different loaders are available38 and you can also write your own if you want to load templates from a36-database or other resources.39+database or other resources. This also enables autoescaping for HTML and40+XML files.41 42 To load a template from this environment you just have to call the43 :meth:`get_template` method which then returns the loaded :class:`Template`::44 6 unmodified lines51 Using a template loader rather than passing strings to :class:`Template`52 or :meth:`Environment.from_string` has multiple advantages. Besides being53 a lot easier to use it also enables template inheritance.54 55+.. admonition:: Notes on Autoescaping56 57+ In future versions of Jinja2 we might enable autoescaping by default58+ for security reasons. As such you are encouraged to explicitly59+ configure autoescaping now instead of relying on the default.60+61+62 Unicode63 -------64 65 Jinja2 is using Unicode internally which means that you have to pass Unicode194 unmodified lines260 .. versionchanged:: 2.4261 262 Jinja2 now comes with autoescaping support. As of Jinja 2.9 the263 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).264+not yet enabled by default though this will most likely change in the265+future. It's recommended to configure a sensible default for266+autoescaping. This makes it possible to enable and disable autoescaping267+on a per-template basis (HTML versus text for instance).268 269+.. autofunction:: jinja2.select_autoescape270+271 Here a recommended setup that enables autoescaping for templates ending272 in ``'.html'``, ``'.htm'`` and ``'.xml'`` and disabling it by default261-for all other extensions::273+for all other extensions. You can use the :func:`~jinja2.select_autoescape`274+function for this::275 263- def guess_autoescape(template_name):264- if template_name is None or '.' not in template_name:265- return False266- ext = template_name.rsplit('.', 1)[1]267- return ext in ('html', 'htm', 'xml')268-269- env = Environment(autoescape=guess_autoescape,276+ from jinja2 import Environment, select_autoescape277+ env = Environment(autoescape=select_autoescape(['html', 'htm', 'xml']),278 loader=PackageLoader('mypackage'))279 280+The :func:`~jinja.select_autoescape` function returns a function that281+works rougly like this::282+283+ def autoescape(template_name):284+ if template_name is None:285+ return False286+ if template_name.endswith(('.html', '.htm', '.xml'))287+288 When implementing a guessing autoescape function, make sure you also289 accept `None` as valid template name. This will be passed when generating274-templates from strings.290+templates from strings. You should always configure autoescaping as291+defaults in the future might change.292 293 Inside the templates the behaviour can be temporarily changed by using294 the `autoescape` block (see :ref:`autoescape-overrides`).295 622 unmodified linesmodified:jinja2/utils.py+7 added-2 removed
520 unmodified lines521 loaded from a string then the default from `default_for_string` is used.522 If nothing matches then the initial value of autoescaping is set to the523 value of `default`.524 525+ For security reasons this function operates case insensitive.526+527 .. versionadded:: 2.9528 """527- enabled_patterns = tuple('.' + x.lstrip('.') for x in enabled_extensions)528- disabled_patterns = tuple('.' + x.lstrip('.') for x in disabled_extensions)529+ enabled_patterns = tuple('.' + x.lstrip('.').lower()530+ for x in enabled_extensions)531+ disabled_patterns = tuple('.' + x.lstrip('.').lower()532+ for x in disabled_extensions)533 def autoescape(template_name):534 if template_name is None:535 return default_for_string536+ template_name = template_name.lower()537 if template_name.endswith(enabled_patterns):538 return True539 if template_name.endswith(disabled_patterns):540 return False84 unmodified linesmodified:tests/test_utils.py+2 added
69 unmodified lines70 assert func('unknown.foo') == 'NONE'71 assert func('foo.html') == True72 assert func('foo.htm') == True73 assert func('foo.txt') == False74+ assert func('FOO.HTML') == True75+ assert func('FOO.TXT') == False76 77 78 @pytest.mark.utils79 @pytest.mark.markupleak25 unmodified lines