Html Twig UTF8 字符编码 - Symfony2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6313255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:04:09  来源:igfitidea点击:

Twig UTF8 Character Encoding - Symfony2

htmlutf-8twig

提问by LBridge

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.

我正在使用 Symfony2 为法国协会的网站开发新闻系统。我在显示重音和 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.

HTML 显示为文本。

I also tried the autoescapefunction (set to trueand false), no success :

我也试过这个autoescape功能(设置为truefalse),没有成功:

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

Any suggestions? Thanks a lot

有什么建议?非常感谢

回答by olive007

First you need setting the charset in your HTML code

首先,您需要在 HTML 代码中设置字符集

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

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

第二个“convert_encoding()”是一个twig函数,它将变量转换为其他编码。

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

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

但也许,您需要在转换变量之前使用 raw

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

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

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

回答by Rafa0809

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'.
  1. HTML 文档:

    <meta charset="UTF-8" />
    
  2. 您使用的文件(通常是控制器和视图)。
  3. 数据库连接。charset 参数必须设置为“utf8”。

回答by Szendvics

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.

尝试将树枝文件和控制器转换为 UTF-8!类似的问题在这里(当将变量从控制器传递给树枝时),这解决了问题。

回答by gonzalon

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

试试这个,如果你在 ddbb 中有这样的东西

&aacute;rbol

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

This will show

这将显示

árbol