Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How remove new lines from string in twig

Tags:

twig

I have this code:

{% set pageDescription = item.description|length > 197 ? item.description|striptags|trim|slice(0, 197) ~ '...' : item.description %}

I will remove new lines from item.description

example I have

<meta name="description" content="An easy playing game about the natural numbers. There will randomly appear the natural. You have to choose what the right after number is. It&amp;#8217;s so easy to play this game, isn&amp;#8217;t it?

The..." />

change to

<meta name="description" content="An easy playing game about the natural numbers. There will randomly appear the natural. You have to choose what the right after number is. It&amp;#8217;s so easy to play this game, isn&amp;#8217;t it? The..." />

How I can do?

Thank you.

like image 408
user3142022 Avatar asked Feb 12 '15 23:02

user3142022


2 Answers

Try using the |replace filter.

|replace({"\n": "", "\r": "", "\t": ""})

For example

{{ item.description|replace({"\n": "", "\r": "", "\t": ""}) }}
like image 126
vitalik_74 Avatar answered Oct 14 '22 03:10

vitalik_74


If you need it only html tag maybe the spaceless {% spaceless %} ... {% endspaceless %} would be enough

like image 21
max4ever Avatar answered Oct 14 '22 03:10

max4ever