My application consists of zones that can have many devices.
When viewing a zone, you must display a control for each device in the zone.
Each device is completely independent, so enclosing device forms in the form of a zone seems unnecessary - I only want to deal with changes on one device at a time.
Currently, I create a form for each device and pass them to the zone view template:
public function viewAction($zone_id) { $zone = $this->getZoneById($zone_id); $forms = array(); foreach ($zone->getDevices() as $device) { $forms[] = $this->createForm(new DeviceType(), $device)->createView(); } return $this->render('AcmeBundle:Zones:view.html.twig', array('zone' => $zone, 'deviceForms' => $forms)); }
And then in the view template, I iterate over the forms:
{% for form in deviceForms %} {% include 'AcmeBundle:Devices:control.html.twig' with {'zone':zone, 'form':form} %} {% endfor %}
It seems that everything is working fine, but I really need to change the template, which is created based on the "type" of the device. What is the cleanest way to do this? I can do something like:
{% if form.vars.data.type == 'foo' %} {% include 'AcmeBundle:Devices:control-foo.html.twig' with {'zone':zone, 'form':form} %} {% elseif form.vars.data.type == 'bar' %} {% include 'AcmeBundle:Devices:control-bar.html.twig' with {'zone':zone, 'form':form} %} {% endif %}
but does it look like there is too much logic in the template? It would be better to assign a template to display the form object in some way, but I don't know if this is possible?