Skip to content

Instantly share code, notes, and snippets.

@icio
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icio/fbcb80e1e8d8b4533150 to your computer and use it in GitHub Desktop.
Save icio/fbcb80e1e8d8b4533150 to your computer and use it in GitHub Desktop.
Twig JavaScript Escaping
{% set text = 'back & for"th' %}
<script>
console.log({
"str0": "{{ text }}",
"str1": "{{ 'back & for"th' }}",
"num1": {{ 123 }},
"str2": "{{ 'back & for"th' | e('html') }}",
"num2": {{ 123 }},
"str3": "{{ 'back & for"th' | e('js') }}",
"num3": {{ 123 | e('js') }},
{% autoescape 'js' %}
"str4": "{{ 'back & for"th' }}",
"num4": {{ 123 }},
{% endautoescape %}
"str5": {{ 'back & for"th' | json_encode }},
"num5": {{ 123 | json_encode }},
"str6": {{ 'back & for"th' | json_encode | raw }},
"num6": {{ 123 | json_encode | raw }},
});
</script>
<script>
console.log({
"str0": "back &amp; for&quot;th",
"str1": "back & for"th",
"num1": 123,
"str2": "back &amp; for&quot;th",
"num2": 123,
"str3": "back\x20\x26\x20for\x22th",
"num3": 123,
"str4": "back & for"th",
"num4": 123,
"str5": &quot;back &amp; for\&quot;th&quot;,
"num5": 123,
"str6": "back & for\"th",
"num6": 123,
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment