filterBy Template and foreach

I am trying to echo multiple subpages with 2 different templates.

<?php
foreach ($page->children()->listed() as $children):
if ($tipptext = $children->filterBy('template', 'tipp-text')):
    echo $tipptext;
endif;
if ($tipp = $children->filterBy('template', 'tipp')):
    echo $tipp;
endif;
endforeach
?>

It’s not working - i don’t get any output

Because filterBy() method returns collection, try to following:

if ($tipptext = $page->children()->listed()->filterBy('template', 'tipp-text')):
    foreach ($tipptext as $subpage):
        echo $subpage->title();
    endforeach;
endif;

To get single page, use findBy() method:

if ($tipptext = $page->children()->listed()->findBy('template', 'tipp-text')):
    echo $tipptext->title();
endif;

If you want to get pages with those two templates

foreach ($page->children()->filterBy('template', 'in', ['tipp', 'tipp-text'] as $subpage) {
echo $subpage->title();
}

That if-statement doesn’t really make sense, you will always get a collection, even if it is empty.

You can however check if the collection is not empty, which usually makes sense if you use a list or whatever to make sure you don’t render any empty tags:

$tipptext = $page->children()->listed()->filterBy('template', 'tipp-text'); // or the solution I suggested above with both templates
if ($tipptext->isNotEmpty()) :
    foreach ($tipptext as $subpage):
        echo $subpage->title();
    endforeach;
endif;
1 Like

Thanks for the answers! This worked for me:

 $items = $page->children()->listed()->filterBy('intendedTemplate', 'in', ['tipp', 'tipp-text']);

The only problem I have now is, that I want to create two different layouts for each template - so I tried this without success:

if ($items == $page->children()->listed()->filterBy('intendedTemplate', 'tipp')) :
        foreach ($items as $subpage):
            echo $subpage->title();
        endforeach;
    else:
    
        if ($items == $page->children()->listed()->filterBy('intendedTemplate', 'tipp-text')) :
        foreach ($items as $subpage):
            echo $subpage->title();
        endforeach;
    endif;

Short explanation: I have a page with two different subpage templates (tipp & tipp-text). If the users choose “tipp”, the output should include an image. “tipp-text” is text only.

I suggest you create two different snippets, tipp.phpand tipp-text.php.

$items = $page->children()->listed()->filterBy('intendedTemplate', 'in', ['tipp', 'tipp-text']);
foreach ($items as $item) {
snippet($item->intendedTemplate()->name(), ['item' => $item]);
}

Then in your snippets, call $item->title() etc. and use your specific markup for each item.

The alternative would be a conditional statement

<?php
$items = $page->children()->listed()->filterBy('intendedTemplate', 'in', ['tipp', 'tipp-text']);
foreach ($items as $item) : ?>

<?php echo $item->title() ?>

<?php if ($item->intendedTemplate()->name() === 'tipp') : ?>
<!-- markup for image-->
<?php endif ?>

<?php echo $item->text()->kt() ?>

<?php endforeach ?>

Thank you! This works flawless.