问题
I pass my template an array of strings which I would like to convert to a jaavascript array:
Controller file (php):
$myVar = array('a','b','c');
Desired html:
var myVar = ["a","b","c"];
I try the following code (twig):
var myVar = ["{{ myVar | join('","') }}"];
But the twig generator converts the quotation marks to html entities and this is the result:
var myVar = ["a","b","c"];
Some idea?
回答1:
You need to apply the raw filter:
var myVar = ["{{ myVar | join('","') | raw }}"];
来源:https://stackoverflow.com/questions/11557010/twig-use-quotation-mark-as-separator-for-join-filter