简体   繁体   English

遍历同一模板中的两个对象

[英]loop through two objects in the same template

I'd like to be able to loop through two objects side-by-side in a template, like this:-我希望能够在模板中并排循环遍历两个对象,如下所示:-

Application.php应用.php

return $twig->render('index.twig', ['fixtures' => $fixtures, 'weathers' => $weathers]);

fixtures & weathers are both arrays containing objects. fixtures & weathers都是包含对象的 arrays。

templates/index.twig模板/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 .应该有 4 行fixtureweather数据在一起 I get 8 rows - 4 with fixtures and 4 with weather .我有 8 行 - 4 行有fixtures ,4 行有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我的数据对于 arrays 个对象来说很尴尬,我无法执行简单的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).假设根据您的问题, fixturesweathers是对应的项目,这意味着链接到fixtureweather应该/将具有相同的索引(如果数组是关联的,则为键)。

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 %}

demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM