Skip to content
Myr

Merge pull request #3051 from ExplodingCabbage/tojson-docs-fixes

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

[Docs] Improve tojson docs (additions, clarifications, & fixes)

authored by David Lord · committed by GitHub · parents 6606f66, 24325ec · Browse files · 2 files+18 added−9 removed
This commit

State

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

Diff stats

2 files+18 added−9 removed

  • other:+18 added−9 removed

2 files+18 added−9 removed

modified:docs/api.rst+4 added-4 removed
310 unmodified lines
311 will have that method called and then the return value is serialized312 as string.313314The :func:`~htmlsafe_dumps` function of this json module is also available315as filter called ``|tojson`` in Jinja2. Note that inside ``script``316tags no escaping must take place, so make sure to disable escaping317with ``|safe`` if you intend to use it inside ``script`` tags unless318you are using Flask 0.10 which implies that:315as a filter called ``|tojson`` in Jinja2. Note that in versions of Flask prior316to Flask 0.10, you must disable escaping with ``|safe`` if you intend to use317``|tojson`` output inside ``script`` tags. In Flask 0.10 and above, this318happens automatically (but it's harmless to include ``|safe`` anyway).319320.. sourcecode:: html+jinja321322 <script type=text/javascript>
511 unmodified lines
modified:docs/templating.rst+14 added-5 removed
108 unmodified lines
109 This function converts the given object into JSON representation. This110 is for example very helpful if you try to generate JavaScript on the111 fly.112113 Note that inside ``script`` tags no escaping must take place, so make114 sure to disable escaping with ``|safe`` before Flask 0.10 if you intend115 to use it inside ``script`` tags:116113 .. sourcecode:: html+jinja114115 <script type=text/javascript>120 doSomethingWith({{ user.username|tojson|safe }});116 doSomethingWith({{ user.username|tojson }});117 </script>118119 It is also safe to use the output of `|tojson` in a *single-quoted* HTML120 attribute:121122 .. sourcecode:: html+jinja123124 <button onclick='doSomethingWith({{ user.username|tojson }})'>125 Click me126 </button>127128 Note that in versions of Flask prior to 0.10, if using the output of129 ``|tojson`` inside ``script``, make sure to disable escaping with ``|safe``.130 In Flask 0.10 and above, this happens automatically.131132Controlling Autoescaping133------------------------134135Autoescaping is the concept of automatically escaping special characters
101 unmodified lines