14

I am developing a news system for a french association's website with Symfony2. I'm having troubles when it comes to displaying the accents and also HTML.

In the twig view I simply output the variable:

{{ article.body }}

If I insert the accent directly in the database like this: 'é', the variable is not even displayed.

If I insert this instead: é it stays the same.

HTML is shown as text.

I also tried the autoescape function (set to true and false), no success :

{% autoescape true %}
    {{ article.body }}
{% endautoescape %}

Any suggestions? Thanks a lot

LBridge
  • 2,135
  • 5
  • 21
  • 32
  • Did you try writing "" in the head of your template? – lgomezma Jun 13 '11 at 10:19
  • 1
    Yes i did. When i type characters directly in the view everything works fine. I also made sure that the database was encoded in UTF-8. – LBridge Jun 14 '11 at 18:53
  • possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – deceze Feb 13 '14 at 13:54
  • article.body comes from the database, right? can you confirm that the accented text is stored correctly in the database? – Mario A Jul 03 '14 at 19:31

4 Answers4

13

First you need setting the charset in your HTML code

<!-- for HTML5 -->
<meta charset="UTF-8" />

Second "convert_encoding()" is a twig function which convert variable to other encoding.

{{ article.body | convert_encoding('UTF-8', 'ISO-8859-1') }}

But maybe, you need to use raw before convert your variable

{{ article.body | raw | convert_encoding('UTF-8', 'ISO-8859-1') }}

http://twig.sensiolabs.org/doc/filters/convert_encoding.html

olive007
  • 760
  • 1
  • 12
  • 32
3

Encoding problem could appear in the next places:

  1. The HTML document:

    <meta charset="UTF-8" />
    
  2. The files you use (controllers and views normally).
  3. The database connection. The charset parameter must be set to 'utf8'.
Rafa0809
  • 1,733
  • 21
  • 24
0

Try this, if you have in the ddbb something like this

&aacute;rbol

{% autoescape %}
  {{ c.data|raw }}
{% endautoescape %}

This will show

árbol
Gonz
  • 1,198
  • 12
  • 26
0

Try to convert the twig files and controllers into UTF-8! The similar problem was here (when passing variables from the controller to twig), and this solved the problem.

Szendvics
  • 92
  • 3