11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.

Home Page:https://www.11ty.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

getNextCollectionItem doesn't work with a dynamically assigned collection

roobottom opened this issue · comments

Describe the bug
I want to grab the next collection item using the getNextCollectionItem filter in Nunjucks. However, if the collection id is dynamic, 11ty throws an error.

To Reproduce
Steps to reproduce the behavior:

  1. set a variable for collection id, eg {% set currentCollection = 'dairy' %} (in my site this comes from my folder level data)
  2. set a variable for next post, ie {% set nextPost = collections[currentCollection] | getNextCollectionItem(page) %}
  3. See error in CLI TypeError: collection is not iterable

Expected behavior
I expect the nextPost var to contain an object of the next post or null

Screenshots
n/a

Environment:

  • OS and Version: Mac, BigSur 11.0.1
  • Eleventy Version: 1.0.0-canary.44

Additional context
n/a

I'm unable to reproduce this (on 0.12.1, Big Sur 11.6.1):

---
# ./src/posts/three.njk
title: Three
---

{%- set previousPost = collections[currentCollection] | getPreviousCollectionItem(page) -%}
{%- set nextPost = collections[currentCollection] | getNextCollectionItem(page) -%}

<h1 data-collection="{{ currentCollection }}">{{ title }}</h1>

prev: {{ previousPost.data.title }}
next: {{ nextPost.data.title }}

And my directory data file, ./src/posts/posts.json:

{
  "tags": ["posts"],
  "currentCollection": "posts"
}

OUTPUT

<h1 data-collection="posts">Three</h1>

prev: Two
next: Four

My first instinct would be to verify the value of currentCollection. I was able to reproduce the error message you mentioned (non-iterable collection) when my collection name was undefined because I had a typo.

Thanks, @pdehaan looks like a late-night dairy (moo!) vs diary issue. I'll close.