Reference sample: restaurant menu

Hi there,

I’ve been wracking my brain tonight trying to figure out how to display the restaurant menu inside my template.
Here you can see the blueprint sample.

<?php

$dishes = $pages->listed();

if($dishes->isNotEmpty()):

?>

    <?php foreach($dishes as $dish): ?>
    <?= $dish->url() ?>">
    <?= $dish->title()->html() ?>

    <?php endforeach ?>
<?php endif ?>

Can someone help me?

During the development of Kirby 3 there was a template, that you can use or change to your needs:

<?php snippet('header') ?>

<main class="restaurant-menu">
  <?php snippet('intro') ?>

  <?php foreach ($categories as $category): ?>
  <section class="dishes">
    <h2><?= $category ?></h2>
    <?php foreach ($page->$category()->toStructure() as $dish): ?>
    <article class="dish">
      <h3 class="dish-name"><?= $dish->dish() ?></h3>
      <p class="dish-description"><?= $dish->description() ?></p>
      <p class="dish-price">€ <?= $dish->price() ?></p>
    </article>
    <?php endforeach ?>
  </section>
  <?php endforeach ?>

</main>

<?php snippet('footer') ?>

For this you need the controller file:

<?php

return function () {

    $categories = [
        'Starters',
        'Pasta',
        'Meat',
        'Desert'
    ];

    return [
        'categories' => $categories
    ];

};

The snippet intro.php looked like:

<header class="intro">
  <h1><?= $page->title() ?></h1>
  <?php if ($page->intro()->isNotEmpty()): ?>
  <div class="intro-text text">
    <?= $page->intro()->kt() ?></p>
  </div>
  <?php endif ?>
</header>
1 Like

Thank you for you fast respons! Where do I need to place the controller file?

Place the controller file as site\controllers\menu.php, if your template is site\templates\menu.php.
The file names must be the same!

1 Like

Thank you. You literally made my day!
Just another small question so that I learn from it. Where is the variable $dish coming from? Can’t find it.

    <?php foreach ($page->$category()->toStructure() as $dish): ?>

Thanks again!

It is defined in this line:

@portobien See foreach loops in the PHP manual: https://www.php.net/manual/en/control-structures.foreach.php

2 Likes

@texnixe:
Thanks for adding the answer of this problem (and more !!!) e.g. to Kirby 3 docs: Example controller and template.

[Added:]
I think you want to delete “&quot;” in front of the file names…

1 Like

@anon77445132 @texnixe Big thumbs up! Thanks