eleventyNavigation | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
{% tableofcontents %}
Fetch the previous and next items in a collection when you pass in the current page
object.
{% codetitle "Liquid", "Syntax" %}
{% raw %}{% assign previousPost = collections.posts | getPreviousCollectionItem: page %}
{% assign nextPost = collections.posts | getNextCollectionItem: page %}{% endraw %}
<!-- in {{ "2.0.0-beta.1" | coerceVersion }} the page argument is optional -->
{% raw %}{% assign previousPost = collections.posts | getPreviousCollectionItem %}
{% assign nextPost = collections.posts | getNextCollectionItem %}{% endraw %}
{% codetitle "Nunjucks", "Syntax" %}
{% raw %}{% set previousPost = collections.posts | getPreviousCollectionItem(page) %}
{% set nextPost = collections.posts | getNextCollectionItem(page) %}{% endraw %}
<!-- in {{ "2.0.0-beta.1" | coerceVersion }} the page argument is optional -->
{% raw %}{% set previousPost = collections.posts | getPreviousCollectionItem %}
{% set nextPost = collections.posts | getNextCollectionItem %}{% endraw %}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? {% include "edit-on-github.njk" %}
Useful when you’d like to link to the previous or next template in your collection:
{% renderFile "./src/_includes/syntax-chooser-tablist.11ty.js", {id: "nextprevlink"} %}{% codetitle "Liquid", "Syntax" %}
{% raw %}
{% if previousPost %}Previous Blog Post: <a href="{{ previousPost.url }}">{{ previousPost.data.title }}</a>{% endif %}
{% if nextPost %}Next Blog Post: <a href="{{ nextPost.url }}">{{ nextPost.data.title }}</a>{% endif %}
{% endraw %}
{% codetitle "Nunjucks", "Syntax" %}
{% raw %}
{% if previousPost %}Previous Blog Post: <a href="{{ previousPost.url }}">{{ previousPost.data.title }}</a>{% endif %}
{% if nextPost %}Next Blog Post: <a href="{{ nextPost.url }}">{{ nextPost.data.title }}</a>{% endif %}
{% endraw %}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? {% include "edit-on-github.njk" %}
The Collections documentation outlines the default sorting algorithm and how to override it.
The getCollectionItemIndex
filter returns the 0-based numeric index of the current (or passed) page in the collection.
{% codetitle "Liquid", "Syntax" %}
{% raw %}
{% assign index = collections.posts | getCollectionItemIndex %}
Or pass it in:
{% assign index = collections.posts | getCollectionItemIndex: page %}
{% endraw %}
{% codetitle "Nunjucks", "Syntax" %}
{% raw %}
{% set index = collections.posts | getCollectionItemIndex %}
Or pass it in:
{% set index = collections.posts | getCollectionItemIndex(page) %}
{% endraw %}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? {% include "edit-on-github.njk" %}
For completeness, a getCollectionItem
filter is also included that fetches the current page from a collection.
{% codetitle "Liquid", "Syntax" %}
{% raw %}{% assign currentPost = collections.posts | getCollectionItem: page %}{% endraw %}
<!-- in {{ "2.0.0-beta.1" | coerceVersion }} the page argument is optional -->
{% raw %}{% assign currentPost = collections.posts | getCollectionItem %}{% endraw %}
{% codetitle "Nunjucks", "Syntax" %}
{% raw %}{% set currentPost = collections.posts | getCollectionItem(page) %}{% endraw %}
<!-- in {{ "2.0.0-beta.1" | coerceVersion }} the page argument is optional -->
{% raw %}{% set currentPost = collections.posts | getCollectionItem %}{% endraw %}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? {% include "edit-on-github.njk" %}