使用Javascript访问通过Twig传递的变量

时间:2022-01-24 23:17:57

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?

我有一个控制器将数组传递给twig模板,我想在该页面上编写的脚本中使用它。我该怎么做呢?

I've tried this in my .twig template:

我在我的.twig模板中试过这个:

<script>
    $(document).ready(function(){
        var test = {{ testArray }};
});
</script>

but that only works if it's a string.

但这只有在它是一个字符串时才有效。

4 个解决方案

#1


137  

You might have to json_encode the array, try this:

你可能需要对数组进行json_encode,试试这个:

<script>
    $(document).ready(function(){
        var test = {{ testArray|json_encode|raw }};
    });
</script>

#2


8  

First, send the data json encoded from controller and

首先,发送从控制器编码的数据json

then in javascript,

然后在javascript中,

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

#3


0  

In My Controller I Install SerializerBundle

在我的控制器中,我安装了SerializerBundle

$serializer = $this->get('serializer');
        $countries = $this->getDoctrine()->getRepository("QSCORBundle:CountryMaps")->findAll();
        $jsonCountries = $serializer->serialize($countries, 'json');
 return $this->render('QSCORBundle:Default:index.html.twig',array("countries"=> $jsonCountries));

And In My File Twig

并在我的文件树枝

<script type="text/javascript" >
 var obj = {{ countries|json_encode|raw }};
 var myObject = eval('(' + obj + ')');

 console.log(myObject[0]['capital_latitude'] + " " + myObject[0]['capital_longitude']);//for the First Element
</script>

#4


0  

I do it this way:

我是这样做的:

Return of the controller test.data then

然后返回控制器test.data

$test = array('data' => array('one','two'))

Twig:

枝条:

<div id="test" data-is-test="{{ test.data|json_encode }}"></div>

Js:

JS:

$(document).ready(function() {
    var test = $('#test').data("isTest");
    console.log(test);
});

Output:

输出:

 ["one", "two"]

documentation here

文档在这里

#1


137  

You might have to json_encode the array, try this:

你可能需要对数组进行json_encode,试试这个:

<script>
    $(document).ready(function(){
        var test = {{ testArray|json_encode|raw }};
    });
</script>

#2


8  

First, send the data json encoded from controller and

首先,发送从控制器编码的数据json

then in javascript,

然后在javascript中,

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

#3


0  

In My Controller I Install SerializerBundle

在我的控制器中,我安装了SerializerBundle

$serializer = $this->get('serializer');
        $countries = $this->getDoctrine()->getRepository("QSCORBundle:CountryMaps")->findAll();
        $jsonCountries = $serializer->serialize($countries, 'json');
 return $this->render('QSCORBundle:Default:index.html.twig',array("countries"=> $jsonCountries));

And In My File Twig

并在我的文件树枝

<script type="text/javascript" >
 var obj = {{ countries|json_encode|raw }};
 var myObject = eval('(' + obj + ')');

 console.log(myObject[0]['capital_latitude'] + " " + myObject[0]['capital_longitude']);//for the First Element
</script>

#4


0  

I do it this way:

我是这样做的:

Return of the controller test.data then

然后返回控制器test.data

$test = array('data' => array('one','two'))

Twig:

枝条:

<div id="test" data-is-test="{{ test.data|json_encode }}"></div>

Js:

JS:

$(document).ready(function() {
    var test = $('#test').data("isTest");
    console.log(test);
});

Output:

输出:

 ["one", "two"]

documentation here

文档在这里