Skip to content
Myr

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

3 files+42 added−18 removed

modified:docs/api.rst+33 added-16 removed
24 unmodified lines
2526The simplest way to configure Jinja2 to load templates for your application27looks roughly like this::2829 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 )3435This will create a template environment with the default settings and a36loader that looks up the templates in the `templates` folder inside the37`yourapplication` python package. Different loaders are available38and you can also write your own if you want to load templates from a36database or other resources.39database or other resources. This also enables autoescaping for HTML and40XML files.4142To 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 lines
51Using a template loader rather than passing strings to :class:`Template`52or :meth:`Environment.from_string` has multiple advantages. Besides being53a lot easier to use it also enables template inheritance.5455.. admonition:: Notes on Autoescaping5657 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.606162Unicode63-------6465Jinja2 is using Unicode internally which means that you have to pass Unicode
194 unmodified lines
260.. versionchanged:: 2.4261262Jinja2 now comes with autoescaping support. As of Jinja 2.9 the263autoescape 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).264not yet enabled by default though this will most likely change in the265future. It's recommended to configure a sensible default for266autoescaping. This makes it possible to enable and disable autoescaping267on a per-template basis (HTML versus text for instance).268269.. autofunction:: jinja2.select_autoescape270271Here a recommended setup that enables autoescaping for templates ending272in ``'.html'``, ``'.htm'`` and ``'.xml'`` and disabling it by default261for all other extensions::273for all other extensions. You can use the :func:`~jinja2.select_autoescape`274function for this::275263 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')268269 env = Environment(autoescape=guess_autoescape,276 from jinja2 import Environment, select_autoescape277 env = Environment(autoescape=select_autoescape(['html', 'htm', 'xml']),278 loader=PackageLoader('mypackage'))279280The :func:`~jinja.select_autoescape` function returns a function that281works rougly like this::282283 def autoescape(template_name):284 if template_name is None:285 return False286 if template_name.endswith(('.html', '.htm', '.xml'))287288When implementing a guessing autoescape function, make sure you also289accept `None` as valid template name. This will be passed when generating274templates from strings.290templates from strings. You should always configure autoescaping as291defaults in the future might change.292293Inside the templates the behaviour can be temporarily changed by using294the `autoescape` block (see :ref:`autoescape-overrides`).295
622 unmodified lines
modified:jinja2/utils.py+7 added-2 removed
520 unmodified lines
521 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`.524525 For security reasons this function operates case insensitive.526527 .. 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 False
84 unmodified lines
modified:tests/test_utils.py+2 added
69 unmodified lines
70 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') == False767778@pytest.mark.utils79@pytest.mark.markupleak
25 unmodified lines