Edit site-wide/reusable content at one place

Hi Kirby-Community!

I’m new to Kirby and playing around with the trail to proof if the concept fit my needs and if I can handle Kirby with my limited programming skills.

What I want to achieve?
I want to build a tiny (5 page) dentist’s site. How I can realise page-wide contents? I want to have e.g. emergency opening hours on every page, but the client should change the content at one particular place.

Best,
Andreas

That’s no problem, let’s suppose your page is on the first level in /content, i.e. /content/opening-hours, then you can reference the page like this from anywhere:

$page = page('opening-hours');
echo $page->title();
//etc.

If your page is on another level,e.g. /content/services/opening-hours just add the path:

$page = page('services/opening-hours');
echo $page->title();
//etc.

If you want it more dynamic, if page can be anywhere:

$page = $site->index()->find('opening-hours');
echo $page->title();
//etc.

If you don’t want to create a separate page, you could put this content into a field openingHours in your site.txt. You can then reference your content like this:

$openingHours = $site->openingHours();
1 Like

Thanks a lot!

I add a new page and this page is invisible (pages: true). The client can change the contents very comfortable via Panel and then I include in the (page or default) template the contents. Right?

Sounds easy. I’ll try it later.

Best,
Andreas

You can additionally use a separate snippet that gets the data from the page and outputs the right markup.
Simply put the template code in site/snippets/openinghours.php and then include the snippet with <?php snippet('openinghours') ?> in all templates that need to display the snippet.

I would use a snippet that shows the contents of that page and then include that snippet in each template where you need it.

1 Like

@lukasbestle beat me to it …

Thank you!

Now I „just“ need to find out how I can print out the values.

openinghours
  label: Öffnungszeiten
  type: structure 
  fields:
    weekday:
      label: Wochentag
      type: select
      options:
        Mo: Montag
…

I tried echo $page->openinghours()->weekday(); and …->label() but nothing appears.

Without the code the content will be output completely and without the formatting for the backend (entry: >

Best,
Andreas

You need to convert the field to a structure object first and can then iterate over all items to print the data:

<?php foreach($page->openinghours()->toStructure() as $item): ?>
  <?php echo $item->weekday(); ?>
<?php endforeach ?>
1 Like

Thanks a lot for your help!

Just one question:

weekday:
  label: Wochentag
    options:
      Mo: Montag

Do you know how I can print out the option ”Montag“ instead of ”Mo“?

Best,
Andreas

The description (“Montag”) is specific to the Panel and can’t be accessed from the templates (the core is completely independent from the Panel and doesn’t care about blueprints).

You can use the following though:

weekday:
  label: Wochentag
    options:
      Montag: Montag
1 Like