0

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.

konrad_firm
  • 859
  • 1
  • 11
  • 28

2 Answers2

3

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

pietro
  • 902
  • 10
  • 22
  • Thanks, but no, it's not a variable, I added a piece of code to my question. – konrad_firm Feb 22 '16 at 09:55
  • thank you very much for your time and help, but it appeared that this is because of my stupidity :( I someday wanted a better formatted html code (more readable), so I did this: stackoverflow.com/questions/35064349/… and there is this piece of code: $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'); that was it. Sorry I wasted your time. – konrad_firm Feb 23 '16 at 06:54
  • 1
    no problem. some problem are simple when you see what is the real problem :) – pietro Feb 23 '16 at 08:11
1

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>
martin
  • 93,354
  • 25
  • 191
  • 226
  • plus, in the first link you gave me, it states "Twig does not escape static expressions:" like `set hello = "Hello"` then why does it convert my string? – konrad_firm Feb 22 '16 at 09:59
  • You're not using static expressions. In your case static expression could be eg. `{{ "alert('ółż');" }}`. When you print `{{ var }}` then it uses escaping as described in the manual. – martin Feb 22 '16 at 10:02
  • but I'm not using variables... I simply do this: `alert('ółż');` – konrad_firm Feb 22 '16 at 10:06
  • using this: `alert(' {{ 'ółż' | raw }}');` (raw filter added) also does not work – konrad_firm Feb 22 '16 at 10:09
  • The theory that it is caused by Twig is only my theory, maybe there is something else in Symfony what can cause this? – konrad_firm Feb 22 '16 at 10:45
  • Maybe try to put the escape block inside ` – martin Feb 22 '16 at 17:38
  • thank you very much for your time and help, but it appeared that this is because of my stupidity :( I someday wanted a better formatted html code (more readable), so I did this: http://stackoverflow.com/questions/35064349/how-to-get-formatted-html-generated-by-symfony/35080486#35080486 and there is this piece of code: `$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');` that was it. Sorry I wasted your time. – konrad_firm Feb 23 '16 at 06:52