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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里