Search code examples
twightmlspecialchars

Twig - use quotation mark as separator for join filter


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?


Solution

  • You need to apply the raw filter:

    var myVar = ["{{ myVar | join('","') | raw }}"];