symfony 2 twig访问数组javascript索引 [英] symfony 2 twig access array javascript index

查看:104
本文介绍了symfony 2 twig访问数组javascript索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Mycontroller

Mycontroller

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));
}

MyTwig

 $(".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}}';

希望这会有所帮助。

Hennes

这篇关于symfony 2 twig访问数组javascript索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆