Use Javascript to access a variable passed through Twig

前端 未结 4 1679
南旧
南旧 2020-11-28 09:24

I have a controller that passes an array to a twig template, which I want to use in a script written on that page. How would I go about doing that?

I\'ve tried this

4条回答
  •  -上瘾入骨i
    2020-11-28 10:29

    First, send the data json encoded from controller and

    then in javascript,

    var context= JSON.parse('{{ YourArrayFromController|raw}}');
    

提交回复
热议问题