Usar Javascript para acceder a una variable pasada a través de Twig
Tengo un controlador que pasa una matriz a una plantilla twig, que quiero usar en un script escrito en esa página. ¿Cómo haría eso?
He intentado esto en mi .plantilla de ramita:
<script>
$(document).ready(function(){
var test = {{ testArray }};
});
</script>
Pero eso solo funciona si es una cadena.
40
Author: ChaoticLoki, 2012-12-18
4 answers
Es posible que tenga que codificar json_encode la matriz, intente esto:
<script>
$(document).ready(function(){
var test = {{ testArray|json_encode|raw }};
});
</script>
143
Author: Supericy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-13 17:00:20
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-13 17:00:20
Primero, envíe los datos codificados json desde el controlador y
Luego en javascript,
var context= JSON.parse('{{ YourArrayFromController|raw}}');
8
Author: user3189566,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-19 09:24:47
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-19 09:24:47
En Mi Controlador Instalo 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));
Y En Mi Archivo Ramita
<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>
0
Author: Mourad MAMASSI,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-17 20:01:06
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-17 20:01:06
Lo hago de esta manera:
Retorno de la prueba del controlador.datos entonces
$test = array('data' => array('one','two'))
Ramita:
<div id="test" data-is-test="{{ test.data|json_encode }}"></div>
Js:
$(document).ready(function() {
var test = $('#test').data("isTest");
console.log(test);
});
Salida:
["one", "two"]
0
Author: shades3002,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-18 23:27:20
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-18 23:27:20