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.
InputArgumentsReturns
Sourceanyno argumentsstringBasicSource
Escapes text for an HTML text node or a quoted attribute value.
Given
{"name": "Tom & \"Jerry\" <b>"}Template
{{ name | escape_html }}Result
Tom & "Jerry" <b>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's a trapNumberSource
Coerces a numeric value to its string form before escaping.
Given
{"count": 42}Template
{{ count | escape_html }}Result
42UnicodeSource
Leaves multi-byte characters untouched and only defuses HTML metacharacters.
Given
{"note": "café ☕ 5 < 6"}Template
{{ note | escape_html }}Result
café ☕ 5 < 6