TBI
TBI

Reputation: 77

Force a line break after white space between words Jinja when inside if block

I am using a framework called CKAN which uses Jinja2 as it's templating language. I am trying to force a line break after every white space in the field.

enter image description here

So it will look like this

Names: test-test-2
             test-test-3
             test-test-4

This is my script:

  {% for key, value in h.format_resource_items(res.items()) %} 
              {% if "Resource Names" == key.title() %}
                  <tr class="tr class"><th scope="row">{{ key.title() }}</th><td>{{ value }}</td></tr>
              {% endif %}
{% endfor %}

I have tried adding this

value|replace(' ', ', ')

and it does work meaning it replaces the white space with a comma and space after but if I try

value|replace(' ', '\n')

it doesn't do anything.

I also tried this

value|replace(' ', <br>)

but it breaks the website and if I put that inside quotation marks it replaces the white space with the actual values <br>

I have also tried, adding an id to the html and then adding CSS but nothing changes either.

Upvotes: 1

Views: 778

Answers (1)

TBI
TBI

Reputation: 77

Found the answer, I needed to add | safe after the replace filter. The safe filter marks the string as safe, allowing it not be automatically escaped if auto escaping is enabled.

{{ value | replace(' ', '<br>') | safe }}

Upvotes: 1

Related Questions