0

I try to use the slice filter in order to limit a link to a specific amount of characters. The field that I render is a link field and my code is the following:

{% set title_text = title|render|striptags %}
<span> 
     {{ title_text|length > 65 ?
         title_text|slice(0,65)  :
         title_text|striptags}}
</span>

Unforturnately some characters like the apostrophe are not rendered appropriately.

So instead of rendering "I've learned", they render "I& #039;ve learned"

1 Answer 1

1

Actually the culprit is |striptags

The problem with using twig's |striptags is double encoding of html entities, not markup, so & becomes & and then &amp; – Berdir

source: How do I get the raw field value in a twig template?

If your setup relies on you running striptags last, you will need to do this all of this magic in a preprocess function with php setting a variable here and then printing your formatted variable on twig.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.