I have a page with two sections “Upcoming” and “Past”. Events will automatically move to “Past” as soon as they are older than the current date.
Now, I would like to show the headline “Upcoming” only when there is an event with an end date in the future.
I thought about “page->hasChildren()”
Does anyone have an idea?
Thanks!
What you would have to do is check if the $upcoming
collection is empty, e.g. assuming your collection with the upcoming event is called $upcoming
:
<?php
if ($upcoming->isNotEmpty()) {
// list the upcoming event
}
If you need more specific help, please post your code.
Thanks @moonwalk,
I am creating the collection after the element which I want to hide. Is it important to create it before the if-statement?
<div class="content-wrapper">
<div class="exhi-content">
Upcoming <--- this should be hidden when $upcoming is empty
<br><br> <--- this should be hidden when $upcoming is empty
<?php
$upcoming = $page
->children()
->filter(function ($child) {
return $child->from()->toDate() > time() || $child->to()->toDate() > time();
})
->sortBy(function ($page) {
return $page->from()->toDate();
}, 'desc');
$past = $page
->children()
->filter(function ($child) {
return $child->to()->toDate() < time();
})
->sortBy(function ($page) {
return $page->from()->toDate();
}, 'desc');
?>
<?php
if ($upcoming->count() > 0): ?>
<ul>
<?php foreach ($upcoming as $event): ?>
<li class="event">
<time>
<?= $event->from()->toDate('j.n') ?> – <?= $event->to()->toDate('j.n.Y') ?> –
</time>
<?= $event->location()->kti() ?>
<br>
<?= $event->name()->kti() ?>
</li>
<br>
<?php endforeach ?>
</ul>
<?php endif ?>
<br>
Past
<br><br>
<?php
if ($past->count() > 0): ?>
<ul>
<?php foreach ($past as $event): ?>
<li class="event">
<time>
<?= $event->to()->toDate('Y') ?> –
</time>
<?= $event->location()->kti() ?>
<br>
<?= $event->name()->kti() ?>
</li>
<br>
<?php endforeach ?>
</ul>
<?php endif ?>
</div>
</div>
Yes, ideally you always put your variable definitions at the top of the template (or into a controller), anyway to keep your templates clean (unless you have to define a variable later, eg. in a loop).