How do I use php to display the information (start date, end date, location, link) “Events” panel sample on GetKirby.com?
Please see the docs: https://getkirby.com/docs/guide/templates/basics
And the other templates in the Starterkit for examples (How to assign blueprints to pages)
Examples in Starterkit:
-
notes.php
templates as an example to loop through the subpages, you would do something similar in theevents.php
template. -
note.php
as an example for a single page, that’s what you would do in yourevent.php
template.
I used this for “events.php”:
And this for “event.php”:
But the date is not showing on the front end.
I need some direction here.
If you used the examples from the docs, there are two date fields, from
and to
, so instead of $event->date()->toDate('d F Y')
you would use those field names $event->from()->toDate('d F Y')
or whatever field names you used.
I am not sure I understand.
This is what I have for “event.yml”
would it be as follows:
<div class="events">
<?php foreach ($page->children()->listed()->sortBy('date', 'desc') as $event): ?>
<article class="event">
<header class="event-header">
<a href="<?= $event->url() ?>">
<h2><?= $event->title() ?></h2>
<time><?= $event->start()->toEnd('d F Y') ?></time>
</a>
</header>
</article>
<?php endforeach ?>
</div>
before I had this, adapted from “note.php”:
<div class="events">
<?php foreach ($page->children()->listed()->sortBy('date', 'desc') as $event): ?>
<article class="event">
<header class="event-header">
<a href="<?= $event->url() ?>">
<h2><?= $event->title() ?></h2>
<time><?= $event->date()->toDate('d F Y') ?></time>
</a>
</header>
</article>
<?php endforeach ?>
</div>
Not toEnd()
, toDate()
is the field method to use.
<time><?= $event->start()->toDate('d F Y') ?></time>
For dates, it’s always the name of the field + the toDate()
field method to convert the value of the field to a date format.
I just tried this and it did not render on the frontend.
There must be something else going on. Please advise.
Do your content files have the correct file name? To render them with the event.php
template, the file name must be event.txt
-
If you are sure everything is correct on that end, is your date fieldsreally called start
?. Please post the event.yml
blueprint.
“event.yml” is:
title: Event
preset: page
pages: true
icon: 📅
num: "{{ page.from.toDate('Ymd') }}"
fields:
from:
label: Start
type: date
width: 1/2
default: today
to:
label: End
type: date
width: 1/2
default: today + 1day
location:
label: Location
type: text
width: 1/2
link:
label: Link
type: url
width: 1/2
text:
label: Description
type: textarea
Well, your fields are called from
and to
like in the example, so you have to use those field names to print the date:
On the single event page:
<?= $page->from()->toDate('Y-m-d') ?>
<?= $page->to()->toDate('Y-m-d') ?>
In the loop
<?php foreach ($page->children() as $event): ?>
<?= $event->from()->toDate('Y-m-d') ?>
<?php endforeach ?>
I’d recommend you go through relevant guide pages to get a better understanding step by step:
Great! I figured it out in some cases by adding “$event”.
Here is “event.php”:
<?php snippet('header') ?><?= $page->title() ?>
<?= $page->from()->toDate('Y-m-d') ?> <?= $page->to()->toDate('Y-m-d') ?> <?php if ($page->tags()->iseventmpty()) : ?><?= $page->tags() ?>
<?php endif ?><div class="event-text text">
<?= $page->text()->kt() ?>
</div>
<div class="events">
<?php foreach ($page->children()->listed()->sortBy('date', 'desc') as $event): ?>
<article class="event">
<header class="event-header">
<a href="<?= $event->url() ?>">
<h2><?= $event->title() ?></h2>
<time><?= $page->from()->toDate('Y-m-d') ?>
<?= $page->to()->toDate('Y-m-d') ?>
</time>
</a>
</header>
</article>
<?php endforeach ?>
</div>
<?php snippet('footer') ?>
and “events.php”:
<?php snippet('header') ?><?= $page->title() ?>
<?= $event->title() ?>
<?= $event->from()->toDate('Y-m-d') ?> <?= $event->to()->toDate('Y-m-d') ?> <?php endforeach ?>Thank you! :-)