One-pager website with exception page

Hi everyone. I’ve made a Kirby one-page website with the following code:

<?php foreach($pages->visible() as $section): ?>
<?php snippet($section->uid(), array('data' => $section)); ?>
<?php endforeach ?>

My question is, what would be a good way to set a page as an exception? It would still appear in the menu, just the content would be on its own page, not be in the one-page.

edit: Perhaps something like this?

<?php foreach($pages->visible()->filter(function($page) {
    return $page->template() != 'news';
}) as $section): ?>
<?php snippet($section->uid(), array('data' => $section)); ?>
<?php endforeach ?>

If it is just one static Page you can use $pages->not($args). If you want the user to be able to set it via the panel you can use filterBy - then you can add a checkbox to your blueprint and filter if it is set or not. (As you just edited your post as I type - yes, something like this ;-))

1 Like

As @Pascalmh already said, you can use not() to exclude a single page/an array of pages/a collection from a page collection. You don’t have to use the filter with callback but you can use filterBy() to filter by template:

Using not():

<?php foreach($pages->visible()->not('news') as $section): ?>
  <?php snippet($section->uid(), array('data' => $section)); ?>
<?php endforeach ?>

Using filterBy():

<?php foreach($pages->visible()->filterBy('template' '!=', 'news') as $section): ?>
  <?php snippet($section->uid(), array('data' => $section)); ?>
<?php endforeach ?>
```
3 Likes

Thanks so much! I don’t really know PHP well, so this helps a lot.

Your solution was perfectly alright, just maybe a bit too complicated :slight_smile:.

Instead of filtering by template, I’d actually prefer intendedTemplate (i.e. the template that should be used according to the content file name) in case the template does not exist.

You mean like this?

<?php foreach($pages->visible()->filter(function($page) {
    return $page->intendedTemplate() != 'news';
}) as $section): ?>
<?php snippet($section->uid(), array('data' => $section)); ?>
<?php endforeach ?>

By the way, I currently appear to have an issue where the new page (the exception) does not appear in the top menu, which I have defined like this:

  <ul class="menu">
    <?php foreach($pages->visible() as $p): ?>
    <li class="parent">
      <a class="menu-link" href="<?php echo url(); e($site->language()->default(), '/#', '/en/#'); echo $p->title() ?>"><?php echo $p->title()->html() ?></a>
    </li>
    <?php endforeach ?>
    <?php foreach($site->languages() as $language): ?>
    <li <?php e($site->language() == $language, ' id="language-active"') ?> class="parent">
      <a href="<?php echo $page->url($language->code()) ?>" class="menu-link">
        <?php echo html($language->name()) ?>
      </a>
    </li>
    <?php endforeach ?>
  </ul>
1 Like

Exactly, that’s what I meant.

As regards the news page missing from the menu, is the page visible?

As far as I know, it is visible.

Here’s a screenshot of my file structure, if that helps.

Yes, there is a folder ‘nieuws’ with a number prepended, but there is also a folder of the same name without a number. Pls remove the folder without the number, that should then solve the issue.

You can’t have two folders with the same name, from a Kirby perspective, “08-nieuws” and “nieuws” would result in the same url, which is not possible.

D’oh. I totally missed that. Thanks again!