how to keep double quotes in twig?

Issue

I have a json array that I pass to the twig template, but the double quotes in the array have been causing trouble, my json array is like this:

$arr = json_encode(array("a", "b")); // which prints out ["a", "b"]

in twig template, I print it out like this:

attrs: {{ arr }}

I expect it to be attrs: ["a", "b"], however, what gets output is attrs: ["a", "b"], I tried attrs: {{ arr|e('js') }}, but no luck, my js lib just says there are some unrecognised characters. So how do I get intended attrs: ["a", "b"]?

Many thanks!

Solution

Sounds like you have auto-escaping on. (e: which is a good thing)

Have you tried {{ arr|raw }} ?

Answered By – Collin Grady

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published