Skip to content

Latest commit

 

History

History
176 lines (123 loc) · 5.64 KB

collection-items.md

File metadata and controls

176 lines (123 loc) · 5.64 KB
eleventyNavigation
parent key title order excerpt
Filters
Next or Previous Collection Item Filters
<code>get*CollectionItem</code>
4
Get next or previous collection items for easy linking.

Get Next or Previous Collection Item Universal Filters

{% tableofcontents %}

getPreviousCollectionItem and getNextCollectionItem {% addedin "0.11.0" %}

Fetch the previous and next items in a collection when you pass in the current page object.

{% renderFile "./src/_includes/syntax-chooser-tablist.11ty.js", {id: "nextprev"} %}

{% 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"} %}

The Collections documentation outlines the default sorting algorithm and how to override it.

getCollectionItemIndex {% addedin "2.0.0-canary.19" %}

The getCollectionItemIndex filter returns the 0-based numeric index of the current (or passed) page in the collection.

{% renderFile "./src/_includes/syntax-chooser-tablist.11ty.js", {id: "getitemindex"} %}

{% 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" %}

getCollectionItem

For completeness, a getCollectionItem filter is also included that fetches the current page from a collection.

{% renderFile "./src/_includes/syntax-chooser-tablist.11ty.js", {id: "getitem"} %}

{% 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" %}