dongshanxun6479 2013-09-04 10:08
浏览 28
已采纳

在TWIG中作为行名称变量:Symfony 2

I have a problem in my Symfony2 application. In Controller I'm getting an array of facebook users and posting it to my form class, where I create some checkboxes. In my template I need to print theese checkboxes as pictures of these users. I try to print it like this : {{form_row(form.{{girl.id}})}}, but TWIG template doesn't allow to use variable {{girl.id}} as name of row and also I don't know how print theese checkboxes as pictures. Can anyone help me with this ?

This is ma Controller action

public function indexAction()
    {

        $facebook = $this->get('facebook');
        $friends = $facebook->api('me/friends?fields=id,name,gender');

        $friends = $friends['data'];
        foreach($friends as $friend){

            if(array_key_exists('gender',$friend))
            {
            if($friend['gender'] =='female')
                $girls[]=$friend;
            }
            }
            for ($i=0; $i <5; $i++) { 
                $default[]=$girls[$i];
            }
        $formular = new FriendsForm();
        $formular->addFriend($default);

        $form = $this->createForm($formular);




        return array('girls'=>$default,'form' => $form->createView());
    }

This is my Form class

class FriendsForm extends AbstractType
{
    private $friends;

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $friends = $this->friends;

        foreach($friends as $friend)
        {
            $builder
            ->add($friend['id'],'checkbox',array('label' => false,'required'=>false, 'value'=>$friend['']));
        ;    
        }

    }


    public function getName()
    {
        return '';

    }

     public function addFriend($data)
    {
        $this->friends = $data;
    }

}

This is my template

<p>
{%if girls is defined%}
{{form_start(form)}}
{%for girl in girls%}
{{form.row(form.{{girl.id}})}}
{%endfor%}
{{form_end(form)}}
{%endif%}
</p>
  • 写回答

2条回答 默认 最新

  • doumi9618 2013-09-04 10:57
    关注

    You can use attribute built-in function (as of 1.2).

    http://twig.sensiolabs.org/doc/functions/attribute.html says

    attribute can be used to access a "dynamic" attribute of a variable

    In such case you can write like below:

    {{ form_row(attribute(form, girl.id)) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题