Hey thanks for your quick reply!
The part which should be reloaded is inside a snippet, which i implementet via a custom block in my homepage (home.twig). So i have a home.php controller:
<?php
return function ($site, $page) {
$limit = 4;
$blogs = $site->children()->filterBy('intendedTemplate', 'blog-overview')->children()->listed()->sortBy('date', 'desc')->paginate($limit);
return [
'limit' => $limit,
'blogs' => $blogs,
'blogspagination' => $blogs->pagination(),
];
};
which works correct. And a home.json.php controller:
<?php
return function ($page) {
$limit = 4;
$blogs = $site->children()->filterBy('intendedTemplate', 'blog-overview')->children()->listed()->sortBy('date', 'desc')->paginate($limit);
$blogspagination = $blogs->pagination();
$more = $pagination->hasNextPage();
$less = $pagination->hasPrevPage();
return [
'blogs' => $blogs,
'more' => $more,
'less' => $less,
'html' => '',
'json' => [],
];
};
and then the javascript:
const ccfblogs = document.querySelector('#ccf-blogs');
const next = document.querySelector('#blogs-next');
const prev = document.querySelector('#blogs-prev');
if (ccfblogs) {
let page = parseInt(ccfblogs.getAttribute('data-page'));
const fetchProjects = async () => {
let url = `${window.location.href.split('#')[0]}.json/page;${page}`;
try {
const response = await fetch(url);
const { html, more, less } = await response.json();
next.hidden = !more;
prev.hidden = !less;
ccfblogs.insertAdjacentHTML('beforeend', html);
page++;
} catch (error) {
console.log('Fetch error: ', error);
}
}
if (next) {
next.addEventListener('click', fetchProjects);
}
}
for my snippet which is rendered as a block in the home.twig:
{% block kd_blog_list %}
<div class="kd-blog-container">
<div id="ccf-blogs" class="row" data-page="{{ blogspagination.nextPage }}" data-aos="fade-in">
{% if blogs is not empty %}
{% for blog in blogs %}
{{ snippet('elements/blog/blog-card', { 'blog': blog } ) }}
{% endfor %}
{% endif %}
</div>
{% block kd_blog_prev %}
{% if blogspagination.hasPrevPage %}
<div id="blogs-prev" class="kd-blog-controll kd-blog-prev d-flex align-items-center justify-content-center">
<i class="fa-solid fa-chevron-left"></i>
</div>
{% endif %}
{% endblock %}
{% block kd_blog_next %}
{% if blogspagination.hasNextPage %}
<div id="blogs-next" class="kd-blog-controll kd-blog-next d-flex align-items-center justify-content-center">
<i class="fa-solid fa-chevron-right"></i>
</div>
{% endif %}
{% endblock %}
</div>
{% endblock %}
I think i make a mistake in declaring the json.
greetings and thanks again.