简体   繁体   中英

how to keep double quotes in twig?

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!

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

Have you tried {{ arr|raw }} ?

如果需要,可以使用 twig 中的escape过滤器将双引号显示到 javascript 中。

{{ array|json_encode|raw|escape('js') }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM