I'd like to be able to loop through two objects side-by-side in a template, like this:-
Application.php
return $twig->render('index.twig', ['fixtures' => $fixtures, 'weathers' => $weathers]);
fixtures
& weathers
are both arrays containing objects.
templates/index.twig
{% extends "frame.twig" %}
{% block content %}
{% for fixture in fixtures and weather in weathers %}
{% include 'fixture.twig' with [{fixture: fixture}, {weather: weather}] %}
{% endfor %}
{% endblock %}
There should be 4 rows with fixture
and weather
data together. I get 8 rows - 4 with fixtures
and 4 with weather
. So, looping twice which is not what I want?
Edit
The goal is something along the lines of:
<div class="border py-2 bg-light mb-3 fixture">
<div class="row justify-content-md-center text-center teams">
<div class="col-5 themed-grid-col text-right font-weight-bold text-uppercase">{{ fixture.homeTeam() }}</div>
<div class="col-2 themed-grid-col">{{ fixture.kickoff() }}</div>
<div class="col-5 themed-grid-col text-left font-weight-bold text-uppercase">{{ fixture.awayTeam() }}</div>
</div>
<div class="text-center font-weight-light location">{{ fixture.location().name() }}</div>
<div class="text-center font-weight-light location">{{ weather.getTemperature() }}</div>
</div>
My data is awkward with arrays of objects where I'm unable to do a simple array_merge
array(4) {
[0]=>
object(\Fixture)#16 (5) {
["id":"\Fixture":private]=>
int(413456)
["awayTeam":"\Fixture":private]=>
string(15) "Birmingham City"
["homeTeam":"\Fixture":private]=>
string(9) "Brentford"
["kickoff":"\Fixture":private]=>
string(5) "15:00"
["location":"\Fixture":private]=>
object(\Location)#17 (3) {
["name":"\Location":private]=>
string(12) "Griffin Park"
["latitude":"\Location":private]=>
float(51.4872912)
["longitude":"\Location":private]=>
float(-0.3036014)
}
}
}
array(4) {
[0]=>
object(\Weather)#22 (8) {
["client":protected]=>
object(\WeatherApiClient)#23 (6) {
["client":protected]=>
object(GuzzleHttp\Client)#24 (1) {
["config":"GuzzleHttp\Client":private]=>
array(8) {
Assuming from your question, fixtures
and weathers
are corresponding items meaning the weather
linked to the fixture
should/would have the same index (or key if the array is associative).
If this is this the case, then you can change your loop to the following:
{% for key, fixture in fixtures %}
{% include 'fixture.twig' with { fixture: fixture, weather: weathers[key]|default } %}
{% endfor %}