1

Standard "when article has no summary, output the first 200 characters of the body minus HTML" logic going on here. We're implementing some Spanish-language features for the first time and, while special characters are stored fine in the database (utf_unicode_ci collation) and they output fine on the front end when convert_encoding isn't applied, they come out garbled when convert_encoding is applied.

My code is:

{{ item.articleBodySpanish.type('bodyText')[0].bodyText|striptags|convert_encoding('UTF-8', 'HTML-ENTITIES')|slice(0, 200)|default('Lee mas') }}...

I have an entry where the database contains this for articleBodySpanish:

<p>Empuja sus puños contra los postes y sigue insistiendo en que ve a los fantasmas.&nbsp;Empuja sus puños contra los postes y sigue insistiendo en que ve a los fantasmas.</p>

With the code above, the output I get is as follows:

Empuja sus puños contra los postes y sigue insistiendo en que ve a los fantasmas. Empuja sus puños contra los postes y sigue insistiendo en que ve a los fantasmas.

If I remove the |convert_encoding('UTF-8', 'HTML-ENTITIES') the output becomes this, so I can't even just take it out to moot the issue:

Empuja sus puños contra los postes y sigue insistiendo en que ve a los fantasmas.&nbsp;Empuja sus puños contra los postes y sigue insistiendo en que ve a los fantasmas.

What can I do here?

1

You shouldn't need any convert_encoding here. From your results, the special characters are coming out fine, it's your &nbsp that is your problem.

For this to go you can use the |raw Twig filter.

2
  • Hi and thanks. Sorry for the delay, had to pivot to another project. I did try {{ item.articleBodySpanish.type('bodyText')[0].bodyText|striptags|raw|slice(0, 200)|default('Lee mas') }} but it still gives me the version with good special characters but uninterpreted &nbsp; ... what am I missing? Thanks again. Mar 7 '19 at 17:54
  • ahh, I didn't actually realize that |raw had to be the last filter. that did it. Mar 8 '19 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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