Thank you so much for the help
I decided to start with following this cookbook and create the pagination first and then to try to apply the filtering too but I stumble upon an error with the pagination after clicking on the ‘add more’ button:
madaf.js:559 GET http://localhost:8000/magazine.json/page:2 404 (Not Found)
Fetch error: SyntaxError: Unexpected token ‘<’, "<!doctype "… is not valid JSON
I did everything exactly as explained with only changing the classes and the variables names.
The other thing that is different is that I use 2 snippets:
I have a magazine page with header and footer and the magazine itself as a snippet on it’s own. and inside the magazine snippet I use anoth er snippet for the magazine-item. Maybe this causes the problem (?)
magazine pgae template:
<?php snippet('header') ?>
<?php snippet( "magazine" ); ?>
<button class="load-more" accesskey="m">Load more</button>
<?php snippet('footer') ?>
the magazine snippet:
<div class="magazine magazine-items-json" id="magazine-items-container" data-href="magazine" data-page="<?= $pagination->nextPage() ?>">
<?php foreach ($items as $item): ?>
<?php snippet( "magazine_item", ['item'=>$item] ); ?>
<?php endforeach ?>
</div>
the magazine_item snippet:
<a href="<?=$item->url()?>" class="mag-item-link">
<div class="mag-item">
<figure class="mag-item-image">
<img src="<?= $item->cover()->toFiles() ?>" alt="">
</figure>
<p class="mag-item-quote"><?= $item->quote()?></p>
<div class="mag-item-info">
<h2 class="mag-item-title"><?= $item->title()?></h2>
<?php if ( $item->intendedTemplate() == "recommend" ) : ?>
<?php if ( $item->name()->isNotEmpty() ) : ?>
<p class="mag-item-artist"><?= $item->name() ?></p>
<?php endif; ?>
<?php elseif ( $item->artist_name()->isNotEmpty() ) : ?>
<p class="mag-item-artist"><?= $item->artist_name() ?></p>
<?php endif; ?>
<p class="mag-item-category"><?= $item->category() ?></p>
</div>
</div>
</a>
magazine controller:
<?php
return function ( $page, $site, $kirby ) {
$limit = 2;
$items = page('magazine')->children()->listed()->paginate($limit);
return [
'limit' => $limit,
'items' => $items,
'pagination' => $items->pagination(),
];
};
magazine json in controllers:
<?php
return function ($page) {
$limit = 2;
$items = page('magazine')->children()->listed()->paginate($limit);
$pagination = $items->pagination();
$more = $pagination->hasNextPage();
return [
'items' => $items,
'more' => $more,
'html' => '',
'json' => [],
'pagination' => $pagination,
];
};
magazine json in templates:
<?php
foreach($items as $item) {
$html .= snippet('magazine_item', ['item' => $item], true);
}
$json['html'] = $html;
$json['more'] = $more;
echo json_encode($json);
js:
if($body.hasClass('magazine-page')){
const element = document.querySelector('.magazine-items-json');
const button = document.querySelector('.load-more');
let page = parseInt(element.getAttribute('data-page'));
const fetchMagItems = async () => {
let url = `${window.location.href.split('#')[0]}.json/page:${page}`;
try {
const response = await fetch(url);
const { html, more } = await response.json();
button.hidden = !more;
element.insertAdjacentHTML('beforeend', html);
page++;
} catch (error) {
console.log('Fetch error: ', error);
}
}
button.addEventListener('click', fetchMagItems);
}