kirby-staticbuilder icon indicating copy to clipboard operation
kirby-staticbuilder copied to clipboard

prev/next navigation

Open flokosiol opened this issue 5 years ago • 4 comments

First of all THANKS for this great plugin!

Are there any know issues with prev/next navigations? I'm using the following code in my template …

<?php if ($page->hasPrevVisible()): ?>
    <a href="<?= $page->prevVisible()->url() ?>">Prev</a>
<?php endif; ?>

<?php if ($page->hasNextVisible()): ?>
    <a href="<?= $page->nextVisible()->url() ?>">Next</a>
<?php endif; ?>

… but it is ignored by the static build. I also tested $page->hasPrev() but this does not work either?! The prev/next navigation works great when the page is rendered dynamically.

I had a look at the issues here on Github, at the documentation and at this forum thread, but unfortunately without luck. Do you have any ideas? Or is there anything I've missed?

I'm using Kirby 2.5.10 and Static Builder 2.2.0.

Thanks in advance.

flokosiol avatar Oct 29 '18 09:10 flokosiol

Not sure what is happening. Is $page->isActive() broken too?

I believe that the root of the issue (and issues like #27) is that Kirby is not really tested to render several pages that share a single Kirby class instance. The "configure" part of the Kirby class is a bit ad-hoc and mostly works in a single HTTP request context. :/

This is the part where we're trying to convince Kirby that the page we're rendering is "active": https://github.com/fvsch/kirby-staticbuilder/blob/b2802f48ee978086661df97992ec27a9e08d0f28/src/Builder.php#L365-L367

Do you know if your use case used to work with older Kirby releases?

fvsch avatar Oct 29 '18 10:10 fvsch

Thanks for the quick reply! Here a some results of tests I made …

$page->isActive()$page->siblings()$page->siblings()->count()$page->siblings()->indexOf($page) (which is used in Kirbys' prev() and next() methods) ❌ page->hasPrevVisible(); page->hasPrev(); page->prev();

I don't know if it works with older versions of Kirby, because I just added the pagination.

I'm trying to find a workaround with $page->num() and $page->siblings(). Maybe this will work 😉

flokosiol avatar Oct 29 '18 10:10 flokosiol

Alright, I found a workaround 🎉

$pageNum = $page->num();
$siblings = $page->siblings(false)->visible();

if ($siblings->count()) {
    $prev = $siblings->findBy('num', $pageNum - 1);
    $next = $siblings->findBy('num', $pageNum + 1);
}

flokosiol avatar Oct 29 '18 11:10 flokosiol

Reopening so that the bug and its workaround are visible in the main issues list.

fvsch avatar Nov 09 '18 18:11 fvsch