php - Variable as name of row in TWIG : Symfony 2 -
i have problem in symfony2 application. in controller i'm getting array of facebook users , posting form class, create checkboxes. in template need print theese checkboxes pictures of these users. try print : {{form_row(form.{{girl.id}})}}, twig template doesn't allow use variable {{girl.id}} name of row , don't know how print theese checkboxes pictures. can me ?
this ma controller action
public function indexaction() { $facebook = $this->get('facebook'); $friends = $facebook->api('me/friends?fields=id,name,gender'); $friends = $friends['data']; foreach($friends $friend){ if(array_key_exists('gender',$friend)) { if($friend['gender'] =='female') $girls[]=$friend; } } ($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 form class
class friendsform extends abstracttype { private $friends; public function buildform(formbuilderinterface $builder, array $options) { $friends = $this->friends; foreach($friends $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 template
<p> {%if girls defined%} {{form_start(form)}} {%for girl in girls%} {{form.row(form.{{girl.id}})}} {%endfor%} {{form_end(form)}} {%endif%} </p>
you can use attribute
built-in function (as of 1.2).
http://twig.sensiolabs.org/doc/functions/attribute.html says
attribute can used access "dynamic" attribute of variable
in such case can write below:
{{ form_row(attribute(form, girl.id)) }}
Comments
Post a Comment