Bootstrap FreeKB - Jinja - Replace newlines with break
Jinja - Replace newlines with break

Updated:   |  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 &lt; (less than) HTML and > into the &gt; (greater than) HTML.

Hello&lt;br /&gt;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 Buy Me A Coffee



Comments


Add a Comment


Please enter 875173 in the box below so that we can be sure you are a human.