Hello,
I’m wondering if I took the right approach in creating a filter menu on my ‘projects’ page of tags of ‘projects’ subpages. (using filtering via routes)
I seems to work fine. But I 'm a php rookie and you never know.
Thanks
config.php
<?php
/**
* config.php
*/
return [
'debug' => true,
'routes' => [
[
'pattern' => 'projects/(:any)',
'action' => function ($tag) {
if ($page = page('projects/' . $tag)) {
return $page;
} else {
return page('projects')->render([
'tag' => $tag
]);
}
}
]
]
];
controllers/projects.php
<?php
/**
* controllers/projects.php
*/
return function ($page, $tag) {
$projects = $page->children()->listed();
// fetch all tags
$tags = $projects->pluck('tags', ',', true);
if ($tag) {
$projects = $projects->filterBy('tags', $tag, ',');
}
return [
'projects' => $projects,
'tags' => $tags
];
};
templates/projects.php
<?php
/*
*templates/projects.php
*/
?>
<?php snippet('header') ?>
<ul class="tags-menu">
<?php foreach ($tags as $tag) : ?>
<li class="tags-item">
<a href="<?= page('projects')->url() . '/' . urlencode($tag) ?>">
<?= $tag ?>
</a>
</li>
<?php endforeach ?>
</ul>
<div class="slideshow noselect">
<div class="swiper-wrapper">
<?php foreach ($projects as $project) : ?>
<div class="swiper-slide">
<picture class="img">
<img src="<?= ($cover = $project->cover()) ? $cover->url() : null ?>" width="<?= $project->cover()->width() ?>" height="<?= $project->cover()->height() ?>" alt="">
</picture>
</div>
<?php endforeach ?>
</div>
</div>
<?php snippet('footer') ?>