Use Javascript to access a variable passed through Twig

前端 未结 4 1680
南旧
南旧 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条回答
  •  春和景丽
    2020-11-28 10:09

    In My Controller I Install 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

    
    

提交回复
热议问题