Html elements not rendering correctly

For some reason gets rendered as much as there are $team_members (while it is put outside the foreach loop),
and is rendered zero times.

(The h2 and img are rendered correctly each time)

How does this work?

In plain php this should be the way to create a main wrapper with as many objects as $team_members contains right?

(First day Kirby)
Thanks for any help!

<?php $team_members = $page->children()->children(); ?>

<div class="team-member-wrapper">
    <?php foreach ($team_members as $member) { ?>
        <div class="team-member">
            <h2><?php echo $member->title() ?></h2>
            <?php echo $member->image() ?>
    <?php } ?>

The syntax is quite right, you dont need the curly braces like plain php…

$team_members = $page->grandChildren();
foreach ($team_members as $member): ?>
  <div class="team-member">
      <h2><?= $member->title() ?></h2>
      <?= $member->image() ?>
<?php endforeach; ?>

p.s welcome to Kirby… it’s awesome :slight_smile:

I was reloading the wrong page while editing my code :smiley:
Seems to work properly indeed.
Thanks anyway jim! Didn’t know about the grandChildren method :slight_smile:

No worries. It is worth reading up on collections, especially if you need to re-use the set of team member pages in other places.

1 Like