Skip to content

Instantly share code, notes, and snippets.

@icio icio/input.twig

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.