Twig - use quotation mark as separator for join filter

和自甴很熟 提交于 2020-01-13 05:50:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!