Im trying to only show projects (or notes) that have a date in the future.
I tried the filteryBy from the docs but im having some trouble.
<?php
$items = $page->children()->filterBy('date', '>', time());
?>
<?php snippet('header') ?>
<main>
<header class="intro">
<h1><?= $page->title() ?></h1>
</header>
<ul class="events"<?= attr(['data-even' => $page->children()->listed()->isEven()], ' ') ?>>
<?php foreach ($page->children()->listed() as $event): ?>
<li>
<a href="<?= $event->url() ?>">
<figure>
<?php if ($cover = $event->cover()): ?>
<?= $cover->crop(800, 1000) ?>
<?php endif ?>
<figcaption><?= $event->title() ?></figcaption>
</figure>
</a>
</li>
<?php endforeach ?>
</ul>
</main>
<?php snippet('footer') ?>
1. Collection::$filters['>'] = [
2. 'validator' => function ($value, $test) {
3. return $value > $test;
4. }
5. ];
texnixe
February 26, 2019, 11:15am
2
You have to do it like this:
$items = $page
->children()
->filter(function ($child) {
return $child->date()->toDate() > time();
});
Thanks, but i still cant seem to get it to work.
Im not using a custom field, just the normal date.
Or am i using the wrong template to test these things out?
<?php
$items = $page
->children()
->filter(function ($child) {
return $child->date()->toDate() < time();
});
?>
<?php snippet('header') ?>
<main>
<header class="intro">
<h1><?= $page->title() ?></h1>
</header>
<ul class="events"<?= attr(['data-even' => $page->children()->listed()->isEven()], ' ') ?>>
<?php foreach ($page->children()->listed() as $event): ?>
<li>
<a href="<?= $event->url() ?>">
<figure>
<?php if ($cover = $event->cover()): ?>
<?= $cover->crop(800, 1000) ?>
<?php endif ?>
<figcaption><?= $event->title() ?></figcaption>
</figure>
</a>
</li>
<?php endforeach ?>
</ul>
</main>
<?php snippet('footer') ?>
title: Event
icon: πΌ
status:
draft: true
listed: true
columns:
- width: 2/3
sections:
images:
type: files
layout: cards
template: image
info: "{{ file.dimensions }}"
image:
ratio: 5/4
cover: true
min: 1
size: small
- width: 1/3
fields:
cover:
type: files
multiple: false
headline:
type: text
date:
label: Date
type: date
description:
type: textarea
tags: true
texnixe
February 26, 2019, 11:49am
4
The date field is now a normal field like any other field.
The problem is that you donβt use your variable $items
in your foreach loop, but loop through the unfiltered collection.
Should be:
<ul class="events"<?= attr(['data-even' => $items)->isEven()], ' ') ?>>
<?php foreach ($items as $event): ?>
I deleted this part and now it seems to work.
Thanks!
<?= attr([βdata-evenβ => $items)->isEven()], β ') ?>