如何使用 json_encode twig 函数在 twig 文件中使用 php json_encode 选项

2022-01-22 00:00:00 php symfony twig

我正在尝试使用 twig json_encode 函数,但是当我这样做时

I am trying to use twig json_encode function but when I do this

    var packageDetails =  {{(packageDetails|json_encode)}};

packageDetails 是一个从控制器传递过来的数组

and packageDetails is an array of array passed from controller

它给了我错误提示

    invalid property id 

因为"所以我想使用转义过滤器;如何使用?

because of " so I want to use escape filter; how do I use it?

推荐答案

仅仅是因为你没有用引号包裹你的输出吗?

Is it simply because you are not wrapping your output in quotes?

var variable = '{{{reference}}}';

更新:

解决问题的实际答案是根据评论将 |raw 添加到标签中

The actual answer to solve the question was adding |raw to the tag as per comments

var packageDetails =  {{(packageDetails|json_encode|raw)}};

相关文章