Reputation: 1884
I have array like this in my controller :
foreach ($entities as $temp) {
$semesters[]['romanic'] = $data1;
$semesters[]['grouping'] = data2;
}
I can render it if my array like this $semesters['romanic'][]
. But for some reason I can't make it like that. I've try several times like this :
{% for semester in semesters %}
{{ semester['romanic'] }}
{{ semester['grouping'] }}
{% endfor %}
or
{% for key, semester in semesters %}
{{ semesters[key]['romanic'] }}
{{ semesters[key]['grouping'] }}
{% endfor %}
I can render this if using 2 loops :
{% for semester in semesters %}
{% for temp in semester %}
{{ temp }}
{% endfor %}
{% endfor %}
But I need to do this with just 1 loop. Is this possible to do?
Upvotes: 0
Views: 58
Reputation: 111859
The first thing you should probably change is creating your array in PHP to:
foreach ($entities as $temp) {
$c = count($semesters);
$semesters[$c]['romanic'] = $data1;
$semesters[$c]['grouping'] = $data2;
}
And in your Twig you can simple use:
{% for item in semesters %}
{{ item.romanic }} {{ item.grouping }}<br />
{% endfor %}
Here's the sample PHP code to test it:
$semesters = array();
$semesters[0]['romanic'] = 1;
$semesters[0]['grouping'] = 2;
$semesters[1]['romanic'] = 3;
$semesters[1]['grouping'] = 4;
$twig->addGlobal ('semesters', $semesters);
echo $twig->render('index.html.twig');
The result is:
1 2
3 4
as expected
Upvotes: 1