Twig Question

Variable as name of row in 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>

Answer

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