I'm trying to make a dynamic twig template to print lists of Entities with different number of fields. The whole point is that I can't print all the columns of $lligues
.
My controller looks like this:
/**
* @Route("/llistarlliga",name="llistar_lliga")
*/
public function llistarLliga(){
$repository = $this->getDoctrine()->getRepository('AppBundle:Lliga');
$camps = array('Nom','Nº equips','Accions');
$lligues = $repository->findAll();
return $this->render('templates/list.html.twig', array('camps' => $camps, 'lligues' => $lligues,
'title' => 'Llistat de lligues'));
}
Twig template:
{# app/Resources/views/forms/lista.html.twig #}
{% extends "templates/home.html.twig" %}
{% block title %}
{{title}}
{% endblock %}
{% block body %}
<table>
<thead>
{% for i in 0..camps|length-1 %}
<th>{{camps[i]}}</th>
{% endfor %}
</thead>
<tbody>
{% for lliga in lligues %}
<tr>
<td>{{lliga.nom}}</td>
<td>{{lliga.numequips}}</td>
<td>
<a href="{{ path('borrar_lliga', {'nom' : lliga.nom}) }}"><img src="{{ asset('images/bin.png') }}" /></a>
<a href="{{ path('modificar_lliga', {'nom' : lliga.nom}) }}"><img src="{{ asset('images/edit.png') }}" /></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
So, I would like to change the second loop (for lliga in lligues) to make it dynamic, so if it has more fields or less, it prints them too.
Thanks to everyone!