Iterate dynamic blueprint data

Hey there…

currently i try to build a block that gets its Informations from another template

so… this is my start:

<?php
   $resourcesfile = $site->page('res-wohnungen');
   $wohneinheiten = $resourcesfile->wohneinheiten()->toStructure()->sortBy('house', 'asc');
?>

if i want some of the data, i iterate through the structure like this:

<?php foreach ($wohneinheiten as $wohneinheit): ?>
  <tr>
    <td><?= $wohneinheit->house(); ?></td>
    <td><?= $wohneinheit->floor(); ?></td>
    <td><?= $wohneinheit->flat(); ?></td>
    <td><?= $wohneinheit->accessibility(); ?></td>
    <td><?= $wohneinheit->wbs(); ?></td>
  </tr>
<?php endforeach ?>

thats how it goes… but i want it to be a little bit more flexible… i want to iterate each “wohneinheit” without manuel-adding fields and sections of the res-wohnungen-blueprint (like house() or flat()).

so i can start searching for them like this:

$tablekeys = $wohneinheiten->first()->content()->keys();

but how to use the resulting array now?

Array
(
    [0] => house
    [1] => floor
    [2] => flat
    [3] => accessibility
    [4] => wbs
}

but how to get a $wohneinheit->house(); without knowing that house() exists but knowing that there is the array $tablekeys?

the following syntax is crap… but it maybe shows, what i am talking about:

<?php foreach ($wohneinheiten as $wohneinheit): ?>
  <tr>
    <?php foreach ($tablekeys as $tablekey): ?>
      <td><?= $wohneinheit->$tablekey(); ?></td> // <-- what for "$tablekey()"???
    <?php endforeach ?>
  </tr>
<?php endforeach ?>

the idea behind this: i want to generate tables with this block… but it is not always known, which columns exist. the block should be usable for pages with different columns/sections/data.

You don’t need the additional $tablekeys array.

<?php foreach ($housingUnits as $housingUnit): ?>
  <tr>
      <?php foreach($housingUnit->content()->toArray() as $value): ?>
        <td><?= html($value) ?></td>
      <?php endforeach ?>
  </tr>
<?php endforeach ?>

This will work as long as you have simple field values that don’t require that you call any field methods on them.

(Sorry for renaming your variables, but I’m allergic to mixed language variable names)

What was the problem with your code? Should work as well?

WTH? you are correct… it works… i am confused… it wasn’t working last night… now i copied it from here and it works… strange

absolutly no need to apologize… you are total correct with it =)

thx a lot… again =)