Frage

I save news text in DB as HTML:

<p>Hello, i'm a text!</p><p> More text</p>

When i render text like this

{{ post.body | raw }}

everything goes well, but i need to truncate text to 20-30 symbols for preview on main page. So, i tried

{{ p.body[:20] ~ '..' }} 

and then text looks like

<p>Hello, i'm..

How can a hide than html tags? "|raw" filter also does not working while truncation. Please help

War es hilfreich?

Lösung

Conveniently, Twig has a built in function for stripping out HTML tags.

The following would output "Hello, i'm a text ..."

{% set some_html = "<p>Hello, i'm a text!</p><p> More text</p>" %}
{{ some_html[:20]|striptags ~ ' ...' }}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top