-->

Twig replaces non-ascii characters with entities

2019-03-03 16:42发布

问题:

Well, that's really strange, Twig (I'm using it with Symfony 3) replaces non - ascii characters (for example "ł") with entities (e.g. ł), but... only in Javascript sections.

I have no idea why and how to disable it.

Edit: yes, I have UTF-8 everywhere, in Nebeans and in HTML head section.

Edit2: here is my current code:

{% autoescape false %}
    <script>
        $(function(){
            alert('ółż');
        })
    </script>
{% endautoescape %}

Even with {% autoescape false %} (as suggested by Martin) it still does it.

回答1:

Hi if the caracters are in a variable it's normal, to disabled you can use :

{{myvar | raw}}

Source if you need : http://twig.sensiolabs.org/doc/filters/raw.html



回答2:

Twig uses different autoescaping strategies based on the context. See manual http://twig.sensiolabs.org/doc/tags/autoescape.html

You can force Twig to disable escaping with:

{% autoescape false %}
    Everything will be outputted as is in this block
{% endautoescape %}

Or for a single print expression with raw filter:

{{ var|raw }}

Also have a look at your Twig 's configuration where autoescaping should be enabled by default.

Edit:

Maybe try also this:

<script>
    {% autoescape false %}
        $(function(){
            alert('ółż');
        })
    {% endautoescape %}
</script>