DocsSintaxTextEscapeescape_html

escape_html

Escapes a value for an HTML text node or a quoted attribute value.

It is not safe for an unquoted attribute, an href or src URL, or content inside <script>, <style>, or an HTML comment. Reach for escape_js, escape_url, or a dedicated sanitizer in those places. Scalar values are coerced to their string form before escaping.

InputArgumentsReturnsanyno argumentsstring
Source
BasicSource

Escapes text for an HTML text node or a quoted attribute value.

Given

{"name": "Tom & \"Jerry\" <b>"}

Template

{{ name | escape_html }}

Result

Tom &amp; &#34;Jerry&#34; &lt;b&gt;
ApostropheSource

Escapes a single quote so the value is safe inside a single-quoted attribute.

Given

{"label": "it's a trap"}

Template

{{ label | escape_html }}

Result

it&#39;s a trap
NumberSource

Coerces a numeric value to its string form before escaping.

Given

{"count": 42}

Template

{{ count | escape_html }}

Result

42
UnicodeSource

Leaves multi-byte characters untouched and only defuses HTML metacharacters.

Given

{"note": "café ☕ 5 < 6"}

Template

{{ note | escape_html }}

Result

café ☕ 5 &lt; 6