Twig UTF8 字符编码 - Symfony2

Twig UTF8 Character Encoding - Symfony2(Twig UTF8 字符编码 - Symfony2)
本文介绍了Twig UTF8 字符编码 - Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

这篇关于Twig UTF8 字符编码 - Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

how to remove this error quot;Response must contain an array at quot; . quot;.quot; while making dropdown(如何删除此错误quot;响应必须在quot;处包含数组。创建下拉菜单时(Q;))
Why is it necessary to use `document.createElementNS` when adding `svg` tags to an HTML document via JS?(为什么在通过JS为一个HTML文档添加`svg`标签时,需要使用`Document.createElementNS`?)
wkhtmltopdf print-media-type uses @media print ONLY and ignores the rest(Wkhtmltopdf print-media-type仅使用@media print,而忽略其余内容)
price depend on selection of radio input(价格取决于无线电输入的选择)
calculate price depend on selection without button(根据没有按钮的选择计算价格)
What should I consider before minifying HTML?(在缩小HTML之前,我应该考虑什么?)