Jinja - Replace newlines with break

by
Jeremy Canfield |
Updated: January 17 2023
| Jinja articles
Let's say you are attempting to replace whitespace with br (line break).
{{ 'Hello World' | replace(" ", "<br/>") }}
And the following is being returned, where the literal <br/> markup is being returned.
Hello<br />World
This occurs because Jinja is formatting < into the < (less than) HTML and > into the > (greater than) HTML.
Hello<br />World
This can be resolved by disabling auto escaping.
{% autoescape false %}
{{ 'Hello World' | replace(" ", "<br/>") }}
{% endautoescape %}
Did you find this article helpful?
If so, consider buying me a coffee over at