symfony 2 twig 访问数组 javascript 索引数组、索引、symfony、twig

2023-09-07 09:44:51 作者:情侣

知道索引是 javascript 变量是否可以从 twig 数组中获取对象

Is it possible to get object from twig array knowing that the index is a javascript variable

我的控制器

public function showContactModalAction($iduser, $profil) {
    $em = $this->getDoctrine()->getEntityManager();
    $contacts = $em->getRepository("LeymaxContactBundle:User")->findBy(array('accesslevel' => 'arch'));

    return $this->render('LeymaxContactBundle:Default:contactModal.html.twig', array('contacts' => $contacts));
}

我的树枝

 $(".fils_zone_livre").on('click', function() {
        var index=3;
        var contacts = '{{contacts[index]}}';

    });

我也试过了:

var contactArray = '{{contacts|json_encode}}';

但我得到了这个回复:

[{},{},{},{},{},{}]

作为一个空数组

推荐答案

只需要和你差不多的.

但不是

var contactArray = '{{contacts|json_encode}}';

你应该使用

var contactArray = '{{contacts|json_encode|raw}}';

希望这会有所帮助.

亨内斯