I explain in more detail in the video below, however, here’s what I’m stuck on.
History
I’m moving away from no-code web builders and want to go back to hand-coding my own site. Something I used to do in the past.
So I’ve managed to rebuild my Webflow hosted website in Kirby locally, and have 2 questions I can’t seem to find an answer to here in the Forums, the Reference on the website, or the Discord
Again, I’m not a pro coder and I don’t do this for a living, I’m just tech-minded and like learning this stuff
1st question
I have a foreach loop and want the last item not to have a spacer under it.
Once you start filtering etc. your collection, you need to pass that collection as parameter to isLast(), otherwise it will not work as expected.
What is child() as in $item->child(), a field? Sorry, don’t really have time to watch a 5 min. video , it’s better if you post more context in the form of code.
<?php
// store the items in a variable for use further down the script
$items = $page->children()->listed()->sortBy('title', 'desc');
foreach ($items as $item): ?>
<?php
// you only want to have the spacer if the item is not the last item
// pass the filtered and sorted collection as param to `isLast()`
// if no parameter is given, all children in the file systems are used in default order
if (!$item->isLast($items)) : ?>
<div class="spacer_100"></div>
<?php endif; ?>
<?php
// you forgot to echo the attribute
if ($item->child() === $page->tools()) :?>
echo 'target="_blank"';
<?php endif; ?>
The last bit could be rewritten shorter like this: