Skip to content
Myr

improve docs about string methods in templates

Before adoption: this commit came in with the history the project adopted, so no landing records it.

closes #273

authored by David Lord · committed by David Lord · parent 1949a5c · Browse files · 1 file+29 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+29 added−12 removed

  • other:+29 added−12 removed

1 file+29 added−12 removed

modified:docs/templates.rst+29 added-12 removed
565 unmodified lines
566When automatic escaping is enabled, everything is escaped by default except567for values explicitly marked as safe. Variables and expressions568can be marked as safe either in:569570a. the context dictionary by the application with `MarkupSafe.Markup`, or571b. the template, with the `|safe` filter570a. The context dictionary by the application with571 :class:`markupsafe.Markup`572b. The template, with the ``|safe`` filter.573573The main problem with this approach is that Python itself doesn't have the574concept of tainted values; so whether a value is safe or unsafe can get lost.574If a string that you marked safe is passed through other Python code575that doesn't understand that mark, it may get lost. Be aware of when576your data is marked safe and how it is processed before arriving at the577template.578576If a value is not marked safe, auto-escaping will take place; which means that577you could end up with double-escaped contents. Double-escaping is easy to578avoid, however: just rely on the tools Jinja2 provides and *don't use builtin579Python constructs such as str.format or the string modulo operator (%)*.579If a value has been escaped but is not marked safe, auto-escaping will580still take place and result in double-escaped characters. If you know581you have data that is already safe but not marked, be sure to wrap it in582``Markup`` or use the ``|safe`` filter.583584Jinja2 functions (macros, `super`, `self.BLOCKNAME`) always return template585data that is marked as safe.586
791 unmodified lines
1378.. code-block:: text13791380 {{ page.title.capitalize() }}13811379This also works for methods on user-defined types.1380For example, if variable ``f`` of type ``Foo`` has a method ``bar`` defined on it,1381you can do the following:1382This works for methods on user-defined types. For example, if variable1383``f`` of type ``Foo`` has a method ``bar`` defined on it, you can do the1384following:13851386.. code-block:: text13871385 {{ f.bar() }}1388 {{ f.bar(value) }}13891390Operator methods also work as expected. For example, ``%`` implements1391printf-style for strings:13921393.. code-block:: text13941395 {{ "Hello, %s!" % name }}13961397Although you should prefer the ``.format`` method for that case (which1398is a bit contrived in the context of rendering a template):13991400.. code-block:: text14011402 {{ "Hello, {}!".format(name) }}140314041405.. _builtin-filters:14061407List of Builtin Filters1408-----------------------
371 unmodified lines