Symfony2 twig - raw and slice filter not working together

In my twig template, I have the following code:

<td>{{ object.content|length > 50 ? object.content|raw|slice(0, 50) ~ '...' : object.content|raw  }}</td>

My object object.contentis a string like this:

<p>Nullam quis risus eget urna mollis ornare vel eu leo. Donec ullamcorper nulla non metus auctor fringilla.</p>

I would like to output a string without tags <p>, <b>, ... Therefore, I add a filter |raw. I also want to print 50 characters of the entire string .

50 character slicing works, but it still shows tags <p>, ...

Now when I do this:

<td>{{ object.content|raw  }}</td>

It shows a string without tags <p>. But when I add the slice filter, it does not work ... I also tried to set the variable before exiting as follows:

{% set rawcontent = object.content %}
<td>{{ rawcontent|slice(0, 50) ~ '...'  }}</td>

But the same result ... How can I fix this?

+4
4

striptags raw

object.content|striptags|slice(0, 50)

. fiddle

+11

: truncate() , :

services:
   twig.extension.text:
      class: Twig_Extensions_Extension_Text
      tags:
          - { name: twig.extension }

:

{{ entity.text|striptags|truncate(50, true, "...")|raw }}

- HTML.

  • , , 'striptags'
  • html, "& eacute" "raw"
  • ;)

    {% if entity.contenu|striptags|raw|length > 50 %}
                    {{ entity.contenu|striptags|truncate(50, true, "...")|raw }}
                {% else %} 
                    {{ entity.contenu|striptags|raw }}
                {% endif %}
    

:

{{ entity.text|striptags|length > 50 ? entity.text|striptags|truncate(50, true, "...")|raw  : entity.text|striptags|raw }}

, ...

+4

"raw", "slice" "~" . ( "striptags" ). "" "" .

, ( , ):

{{ event.info|length > 300 ? event.info|slice(0,300)|raw : event.info|raw }}
{{ event.info|length > 300 ? '...' : ''}}
+2
source

I tried the code of Max Lipsky, then I noticed that it does not work for such characters as &atilde;, after which I changed it a bit and now it works fine.

{{ event.info|striptags|length > 150 ? event.info|raw|slice(0,150)|raw : event.info|raw }}
{{ event.info|striptags|length > 150 ? '...' : ''}}
+1
source

All Articles