I got a two questions about foreach loop items

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 :grimacing:

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 :v:

1st question
I have a foreach loop and want the last item not to have a spacer under it.

So I have:

<?php if ($item->isLast()) { } else { ?>
<div class="spacer_100"></div>
<?php } ?>

The code doesn’t break my site but doesn’t do what I want it to do.

2nd question
Within the same foreach loop I only want my ‘Tools’ CMS collection items to have a target="_blank" in the link.

So I have:

<?php if ($item->child() === $page->tools()) { ?>
target="_blank"
<?php } ?>

Again, not a broken page, but not working.

What am I doing wrong with the two code snippets above?

Here’s a link to what I’m trying to do: :point_right: https://cln.sh/sO702J

Thanks in advance if anyone can help :nerd_face::+1:

Kis.

Hi,

a solution for your first questionn could be the following code.

<?php if(!$item->isLast()): ?>
  <div class="spacer_100"></div>
<?php endif; ?>

:wave:
David

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 :woman_shrugging:, it’s better if you post more context in the form of code.

And welcome to the forum!

Hey @texnixe, thanks and understood.

Here’s the full loop:

    <?php foreach ($page->children()->listed()->sortBy('title', 'desc') as $item): ?>
        
        <div class="cms_item_container">
            
            
<!-- UNDER CONSTRUCTION -->

            <h2><a href="<?= $item->url() ?>" title="<?= $item->page_header() ?>" 
            
            <?php if ($item->child() === $page->tools()) { ?>
                        target="_blank"
            <?php } ?>
            
            ><?= $item->page_header() ?></a></h2>
            
<!-- UNDER CONSTRUCTION -->                        
            
            <div class="cms_content">
                <?= $item->page_content() ?>

            </div>
            <div class="tags_container">
                <span class="tags"><?= $item->select()->option() ?></span>
                <?php if ($item->affiliate() == "true") { ?><span class="tags">Affiliate</span><?php } ?>
                
            </div>
        </div>
        
<!-- UNDER CONSTRUCTION -->
        
        <?php if ($item->isLast()) { } else { ?>
        <div class="spacer_100"></div>
        <?php } ?>
        
<!-- UNDER CONSTRUCTION -->                    
        
    <?php endforeach ?>

Thanks for that @David, but it didn’t work either :grimacing: I’ll play with it as your snippet makes me understand it a little more now :nerd_face::+1:

    <?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:

<?= ($item->child() === $page->tools()) ? ' target="_blank"' : '' ?>

Just on a side note, I would like to point you to this: Kirby templates 101 | Kirby CMS

Thank you so much for this @texnixe :pray:

All worked and love the comments, this is really gonna help me out!

Checking the Templates 101 page now :nerd_face::+1: