Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig UTF8 Character Encoding - Symfony2

Tags:

html

utf-8

twig

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

like image 852
LBridge Avatar asked Jun 11 '11 00:06

LBridge


2 Answers

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'.
like image 69
Rafa0809 Avatar answered Sep 28 '22 10:09

Rafa0809


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

like image 32
olive007 Avatar answered Sep 28 '22 08:09

olive007