eleventy
eleventy copied to clipboard
How to use getPreviousCollectionItem or getNextCollectionItem with Handlebars templates
There isn't an example for this in the documentation, nor anywhere else I can find online. Nobody seems to use these in any handlebars theme.
But I can't see how using these options in handlebars is even possible, since you can't set variables like you do in Nunjucks. But I also can't see anything in the documentation that says they don't work.
I've spent many hours last night trying to work out how.
Sorry about that Richard!
Does this work? (via https://www.11ty.dev/docs/languages/handlebars/#supported-features)
{{ getPreviousCollectionItem collections.posts }}
{{ getNextCollectionItem collections.posts }}
<!-- pass in page -->
{{ getPreviousCollectionItem collections.posts page }}
{{ getNextCollectionItem collections.posts page }}
Beyond that usage, I’d expect Subexpressions to be the way forward to operate on those! https://handlebarsjs.com/guide/expressions.html#subexpressions
In case anyone else has the same problem and lands on this page:
{{> includes/previous related=(getPreviousCollectionItem collections.post page) }}
{{> includes/next related=(getNextCollectionItem collections.post page) }}
So that uses a previous and next partial to render the previous and next items, and passes that item via the key related to those partials.
Then you can do this in the partial:
<a href="{{ related.url }}">{{ related.data.title }}</a>
This is an automated message to let you know that a helpful response was posted to your issue and for the health of the repository issue tracker the issue will be closed. This is to help alleviate issues hanging open waiting for a response from the original poster.
If the response works to solve your problem—great! But if you’re still having problems, do not let the issue’s closing deter you if you have additional questions! Post another comment and we will reopen the issue. Thanks!