简体   繁体   中英

Symfony 3.4 : Render multiple array in twig

I got a form with CollectionType that stores an array in an SQLite database, this is the structure of the entity object returned in the controller:

Expertations {#666 ▼
-id: 9
  -date: DateTime @1536749818 {#653 ▶}
  -client: 1
  -status: 0
  -price: 0.0
  -expiration: DateTime @1536749818 {#650 ▶}
  -tipo: 1
  -kw: 12
  -piani_casa: 2
  -riscaldamento: "1"
  -opere_murarie: false
  -trifase: false
  -sconto: 10.0
  -level: 1
  -square_meters: 140
  -floor: array:6 [▼
    0 => 1
    1 => 1
    2 => 2
    3 => 2
    4 => 2
    5 => 2
  ]
  -ambient: array:6 [▼
    0 => Rooms {#671 ▼
      -id: 10
      -name: "Cucina"
      -level: 1
      -sq_meter_from: 0.0
      -sq_meter_to: 999.0
      -punti_prese: 5
      -punti_luce: 1
      -prese_tv: 1
    }
    1 => Rooms {#649 ▶}
    2 => Rooms {#670 ▶}
    3 => Rooms {#669 ▶}
    4 => Rooms {#649 ▶}
    5 => Rooms {#668 ▶}
  ]
  -name: array:6 [▼
    0 => "Cucina"
    1 => "Soggiorno"
    2 => "Corridoio"
    3 => "Camera Padronale"
    4 => "Camera Figlia"
    5 => "Bagno"
  ]
  -pp: array:6 [▼
    0 => 5
    1 => 4
    2 => 2
    3 => 4
    4 => 4
    5 => 2
  ]
  -pl: array:6 [▼
    0 => 1
    1 => 1
    2 => 2
    3 => 1
    4 => 1
    5 => 2
  ]
  -pt: array:6 [▼
    0 => 1
    1 => 1
    2 => 0
    3 => 1
    4 => 1
    5 => 0
  ]
  -num_circuiti: 5
  -num_prese_telefono_dati: 3
  -illum_sicurezza: 2
  -spd: 1
  -imp_ausiliari: 1
}

This should be rendered in a twig template as a table, much of the data is retrieved using a simple array access like {{ item.string }} . The fields named floor, ambient, name, pp, pl, pt should be rendered in the column, one row for item (you should see that in this example all the elements contains 5 keys).

I've tried to access the array as usual but I got an error relating trying to access a key that in reality is an integer (shouldn't made?)

here is the twig for loop that don't work:

{% for items in item %}
<tr>
    <td>{{ items.floor }}</td>
    <td>{{ items.ambient }}</td>
    <td>{{ items.name }}</td>
    <td>{{ items.pp }}</td>
    <td>{{ items.pl }}</td>
    <td>{{ items.pt }}</td>
</tr>
{% endfor %}

The error returned is: Impossible to access an attribute ("floor") on a integer variable ("1").

Expected Behaviour:

These items should be rendered in the table, eg: row one, show floor.0 value, ambient.0.name, name.0 value, pp.0 value, pl.0 value, pt.0 value`, column two etc..

Will someone got the solution to render it correctly?

For a single Expertations object (named yourObject) the loop should look like this:

{% for key in yourObject.floor|keys %}
    <tr>
        <td>{{ yourObject.floor[key] }}</td>
        <td>{{ yourObject.ambient[key] }}</td>
        <td>{{ yourObject.name[key] }}</td>
        <td>{{ yourObject.pp[key] }}</td>
        <td>{{ yourObject.pl[key] }}</td>
        <td>{{ yourObject.pt[key] }}</td>
    </tr>
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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