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
Provenance
- Commit
d2bf582- Tree
1e8e4e0- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
1949a5c
1 file changed+29 added−12 removed
modified:docs/templates.rst+29 added-12 removed
565 unmodified lines566 When automatic escaping is enabled, everything is escaped by default except567 for values explicitly marked as safe. Variables and expressions568 can be marked as safe either in:569 570-a. the context dictionary by the application with `MarkupSafe.Markup`, or571-b. the template, with the `|safe` filter570+a. The context dictionary by the application with571+ :class:`markupsafe.Markup`572+b. The template, with the ``|safe`` filter.573 573-The main problem with this approach is that Python itself doesn't have the574-concept of tainted values; so whether a value is safe or unsafe can get lost.574+If a string that you marked safe is passed through other Python code575+that doesn't understand that mark, it may get lost. Be aware of when576+your data is marked safe and how it is processed before arriving at the577+template.578 576-If a value is not marked safe, auto-escaping will take place; which means that577-you could end up with double-escaped contents. Double-escaping is easy to578-avoid, however: just rely on the tools Jinja2 provides and *don't use builtin579-Python constructs such as str.format or the string modulo operator (%)*.579+If a value has been escaped but is not marked safe, auto-escaping will580+still take place and result in double-escaped characters. If you know581+you have data that is already safe but not marked, be sure to wrap it in582+``Markup`` or use the ``|safe`` filter.583 584 Jinja2 functions (macros, `super`, `self.BLOCKNAME`) always return template585 data that is marked as safe.586 791 unmodified lines1378 .. code-block:: text1379 1380 {{ page.title.capitalize() }}1381 1379-This also works for methods on user-defined types.1380-For example, if variable ``f`` of type ``Foo`` has a method ``bar`` defined on it,1381-you can do the following:1382+This 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 the1384+following:1385 1386 .. code-block:: text1387 1385- {{ f.bar() }}1388+ {{ f.bar(value) }}1389 1390+Operator methods also work as expected. For example, ``%`` implements1391+printf-style for strings:1392 1393+.. code-block:: text1394+1395+ {{ "Hello, %s!" % name }}1396+1397+Although you should prefer the ``.format`` method for that case (which1398+is a bit contrived in the context of rendering a template):1399+1400+.. code-block:: text1401+1402+ {{ "Hello, {}!".format(name) }}1403+1404+1405 .. _builtin-filters:1406 1407 List of Builtin Filters1408 -----------------------371 unmodified lines