2

I've been having problem displaying accented characters (and somtimes apostrophes too) in twig. The site's been recently hosted and I was experiencing none of this on my local server. This problem's been encountered before (there's a question about it here but using raw doesn't work for me). In twig, I have something like this:

{{ entry.textFr|striptags|raw }}

Despite using the raw filter, I can't get those characters to be displayed right (I'm also aware that the raw tag is dangerous but I fully trust the content of the entry entity).

More profoundly, I'm using symfony2 and stfalcon's tinymce bundle, which seems to convert those characters automatically before they are persisted in the db. None of this was the case on my local server.

Does someone have an idea?

Edit

In particular, it seems that 'é', 'è', 'à', 'ê', 'ô', as well as ellipsis and apostrophes are displayed as é, è, à, etc. I've just tried using the replace filter like this |replace({é': 'é', 'è': 'è', 'à': 'à', //ect }) and it seems to work but it's kind of dirty

Edit 2

Nevermind. I just made a twig filter with html_entity_decode. Didn't wanna use that solution but couldn't find anything else.

Community
  • 1
  • 1
Afunakwa
  • 437
  • 1
  • 7
  • 20
  • How is your browser encoding? It can depend on those settings. Also your markup head defines which encoding to use. – DerStoffel Jun 02 '15 at 06:31
  • You should check your encoding, usualy you have a sort of encoding parameters in config.yml – Alexandru Olaru Jun 02 '15 at 06:32
  • The markup head defines the charset as utf-8. This is also the case for doctrine in config.yml. I see no configuration option related for twig itself and as far as I'm aware, it's not my browser that's problematic, since the issue is consistent across browsers and users – Afunakwa Jun 02 '15 at 06:54
  • Apparently it comes down to the striptags filter http://stackoverflow.com/questions/28670146/twig-striptags-and-html-special-chars – DerStoffel Jun 02 '15 at 07:38
  • Removing the striptags filter doesn't remove the problem – Afunakwa Jun 02 '15 at 09:06
  • It would help plenty, if you could provide the characters you want to display. This is just guessing in the dark. Please provide an example in your question and I'll have a look at it more thoroughly. – DerStoffel Jun 02 '15 at 09:22
  • I was missing `|raw` and now the accented characters are well displayed. Thanks. – Pathros Mar 09 '18 at 19:36

2 Answers2

11

I had the same problem, and as I see, lots of people are still having it. After long researches, I solved with Convert Encoding this way:

{{ mytext|striptags|trim|convert_encoding('UTF-8', 'HTML-ENTITIES') }}

("trim" can be omitted for the post purpose, it was just used to have a cleaner string, in my case)

Jacopo Pace
  • 460
  • 7
  • 14
0

Working with Laravel - October CMS I solved this issue adding a charset metatag in each file, the parent with "head" closure and childrens without.

d3punts
  • 1
  • 1