Twig UTF8 Character Encoding - Symfony2

后端 未结 4 1164
青春惊慌失措
青春惊慌失措 2021-01-02 01:27

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 t

相关标签:
4条回答
  • 2021-01-02 01:56

    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

    0 讨论(0)
  • 2021-01-02 01:59

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

    &aacute;rbol
    
    {% autoescape %}
      {{ c.data|raw }}
    {% endautoescape %}
    

    This will show

    árbol
    
    0 讨论(0)
  • 2021-01-02 02:02

    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.

    0 讨论(0)
  • 2021-01-02 02:12

    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'.
    0 讨论(0)
提交回复
热议问题