繁体   English   中英

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

[英]Variable as name of row in TWIG : Symfony 2

我的Symfony2应用程序有问题。 在Controller中,我得到了一系列的Facebook用户,并将其发布到我的表单类中,在其中创建了一些复选框。 在我的模板中,我需要将这些语言的复选框打印为这些用户的图片。 我尝试像这样打印它:{{form_row(form。{{girl.id}})}},但是TWIG模板不允许将变量{{girl.id}}用作行名,我也不想这样做不知道如何将这些复选框打印为图片。 谁能帮我这个 ?

这是控制器操作

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

这是我的形式课

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

}

这是我的模板

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

您可以使用attribute内置功能(从1.2开始)。

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

属性可用于访问变量的“动态”属性

在这种情况下,您可以这样写:

{{ form_row(attribute(form, girl.id)) }}

你可以写成

{{form.row(form[girl.id])}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM