Open cawa-93 opened 2 years ago
Interesting, it seems to work if I hardcode the pagination.data
to collections.posts
in the layout file, but it really doesn't seem to like using eleventyComputed
data for pagination.
---
layout: layouts/base.njk
pagination:
data: collections.posts
alias: posts
size: 2
reverse: true
coll:
- data:
title: one
- data:
title: TWo
- data:
title: three
eleventyComputed:
coll2:
- data:
title: FOur
- data:
title: FIve
- data:
title: SIx
---
postTag={{ postTag }}
postCollection={{ postCollection }}
posts={{ posts }}
{% for post in posts %}
<h2>{{ post.data.title | inspect | safe }}</h2>
{% endfor %}
Both data: collections.posts
and data: coll
seem to work fine.
But as soon as I try using data: coll2
I get the following error:
[11ty] Could not find pagination data, went looking for: coll2 (via Error)
OK, I think I found a workaround (for Eleventy 1.0.1+).
Looks like 1.0.1 added a second parameter to the before()
callback, so now we can access both the paginationData
and fullData
cascade.
---js
{
layout: "layouts/base.njk",
pagination: {
data: "collections.all",
alias: "posts",
size: 2,
reverse: true,
before(paginationData, fullData) {
// Ignore the original pagination data set (`collections.all`) and
// return the specified `collections[postTag]` items.
return fullData.collections[fullData.postTag];
}
}
}
---
<h1>{{ postTag }} Archive</h1>
{% for post in posts %}
<h2>{{ post.data.title }} -- {{ post.url }}</h2>
{% endfor %}
Now my /index.njk looks like this, which will paginate over all the collections.posts
items:
---
layout: layouts/post-feed.njk
postTag: posts
---
And I can create a /blog.njk file which sets the postTag
to "blog" and will paginate over all the collections.blog
items:
---
layout: layouts/post-feed.njk
postTag: blog
---
I think this is one of those pitfalls documented at the very top of the Computed Data feature: https://www.11ty.dev/docs/data-computed/
Computed Data happens after pagination.
However! I do think this is fixable for this specific use case! We could look at the pagination data
property and see if it’s being set by computed data? So the options right now are either to use the workaround @pdehaan posted and/or put it into the enhancement queue for later.
I'm finding that from v2.0.0-canary.9 my Computed Data
in paginated items are breaking. I wonder if this is connected? No errors in the console. Just not outputting in the html.
pagination: {
data: "companies",
size: 1,
alias: "company"
},
eleventyComputed: {
title: "{{ company.name | safe }}"
}
no title in the html.
v2.0.0-canary.8 is fine 👍
However! I do think this is fixable for this specific use case! We could look at the pagination
data
property and see if it’s being set by computed data? So the options right now are either to use the workaround @pdehaan posted and/or put it into the enhancement queue for later.
@zachleat, could you add this one to the 2.0.0 milestone? Or where could any vote for it? 😄
I'm trying to create a few pages that will display posts filtered by some tag. To do this, I created a shared layout that accepts an array of records. For example, the home page should display all posts with the tag "post".
index.njk
However, I can not transfer data to pagination
layouts/post-feed.njk