How to check if if we're on the right page

It seems that I’m almost there but need the last bit to get it all working…

Instead of creating a lot of templates for similar pages I want to have a simple conditional which checks the uuid of the current page. Based on that condition I show a section, like an overview of productions, or the playdates of said productions.

This is what I have now, which unfortunately doesn’t work. If someone can steer me in the right direction it would be awesome :slight_smile:

<?php // first we check if we're on the calendar page
  if ($page->uuid() === 'page://sPZAnprGa36jrSYv'): ?>
    <h2><?= t('playdates') ?></h2>
    // we then target the productions which all are children of the productions page
    $productions = $site->find('page://YDLYvueOhZwB3glM')->children()->listed();
    // we loop through each of them to reach their data
    foreach ($productions as $production):
      // using the `toStructure()` method, we create a structure collection
      $items = $production->playdates()->toStructure();
      // we loop through the entries and render the individual fields
      foreach ($items as $item): ?>
          <?= $item->playdate()->toDate('EEEE d MMMM Y – H:mm') ?>
          <?= $production->title() ?>
          <?= $item->location() ?>
          <?= $item->city() ?>
          <?php if ($item->ticketlink()->isNotEmpty()): ?>
            <a href="<?= $item->ticketlink() ?>">tickets</a>
          <?php endif ?>
      <?php endforeach ?>
    <?php endforeach ?>
  <?php endif ?>

The first conditional if ($page->uuid() === 'page://sPZAnprGa36jrSYv') doesn’t work. Without this conditional the list is rendered, but on all pages. I like to only show it when we’re on the calendar page.

I think you have to check against $uuid->toString() | Kirby CMS, otherwise it’s an object.


I’m not quite sure how to implement that; there are no samples on the linked reference. When I test <?= $uuid->id() ?> I get an error: “undefined variable $uuid”…

In your code, change this

if ($page->uuid() === 'page://sPZAnprGa36jrSYv'): ?>

to this

if ($page->uuid()->toString() === 'page://sPZAnprGa36jrSYv'): ?>

You’re missing the $page here.

Ha! It works. Lovely. There is a lot to learn but it feels very transparent the way Kirby is set up. Thank you @stffr !