Is it possible to load different templates in foreach loop?

Good day everyone,

I got this situation, I have a page with these subpages:

From this 4 pages, the Page “Träger” has content that is differently structured than another pages.

Now on main page template I have a foreach loop

  <?php foreach($page->children() as $article): ?>
    <h2><?= $article->title() ?></h2>
      $items = $article->person()->toStructure();
      foreach ($items as $item): ?>
      <h3><?= $item->name()->html() ?></h3>
      <?= $item->about()->kirbytext() ?>
    <?php endforeach ?>
  <?php endforeach ?>

But the problem is, I’d love to use an entirely different template for “Träger” subpage. The example above is fitting for all other pages, but not for “Träger” page. Thus, my question, is it possible to differentiate templates inside foreach loop.

Well, you can either use an if condition:

<?php foreach($page->children() as $article): ?>
    <h2><?= $article->title() ?></h2>
    <?php if ($article->template()->name() === 'xxx'): ?>
        <!-- stuff in if case -->
    <?php else: ?>
        <!-- stuff in else case -->
    <?php endif ?>
<?php endforeach ?>

Or -assuming your subpages use blueprints x and y, you create two snippets x and y and call these by intended template.

<?php foreach($page->children() as $article): ?>
    <h2><?= $article->title() ?></h2>
    <?php snippet($article->intendedTemplate()->name()) ?>
<?php endforeach ?>

Thanks for quick reply! I’ve created two snippets, corresponding to the templates described above.

The snippet, that should be called by intendedTemplate(), looks like this :


<article class="pad--small">
  <?php foreach($page->children()->listed() as $person): ?>
  <h2><?= $person->title() ?></h2>
  <?= $person->position()->kt() ?>
  <?= $person->about()->kt() ?>
  <?php endforeach ?>

but nothing is displayed, it should take Information of subpages with templates although the content is there.
The page structure is something like this

- Team
-- Team A (Schulleitung...) / template-team
--- Person A1 / template-person
--- Person A2 / template-person
--- ...
-- Team B (Eltern) / template-team
--- Person B1 / template-person
--- Person B2 / template-person
--- ...
-- Sponsors (Träger) / template-sponsors
--- Company 1 / template-company
--- Company 2 / template-company
--- ...
-- Team C (Verein) / template-team
--- Person C1 / template-person
--- Person C2 / template-person
--- ...

You have to pass the $article(of the loop) as $page to the snippet:

<?php foreach($page->children() as $article): ?>
    <h2><?= $article->title() ?></h2>
    <?php snippet($article->intendedTemplate()->name(), ['page' => $article]) ?>
<?php endforeach ?>
1 Like

Unfortunately, it’s not working for me. First, I’m going to try to debug it, when it’s not helping I’m going to use the if else road :slight_smile:

I corrected my error above, falsely used $page->intendedTemplate()->name() inside the loop instead of $article->intendedTemplate()->name():

1 Like

Now it works great, much more elegant solution than grinding through if else loop :star_struck: