-
I have an object that will contain keys and I don't know the keys in advance. I'm using pagination to create a page for each key. How can I also paginate those pages? Let's say I have the following data: postsByAuthor = {
alice: ["post_one", "post_two", "post_three"],
bob: ["post_one", "post_two", "post_three"],
charlie: ["post_one", "post_two", "post_three"],
} I'm creating a page for each author like this: ---
pagination:
data: postsByAuthor
size: 1
alias: author
permalink: "authors/{{author}}/posts/"
--- Is it possible to also have the pages for each author be paginated? Basically, can I create the page |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You can, but it's not trivial. Take a look at the Collections API. You'll want to create an authors collection (you might already have one). And then a postBy collection for each author. You can then paginate the authors collection to create a page for each author. And paginate the postBy collectons to chunk the authors posts by however many you want on each author page. To loop an object you can use Object.entries() |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply, @dwkns! I'm still not able to see how I could create the paginated pages for the postBy collections, though. I see how I could create a collection for each author by looping through an array of the author's names. But I don't see how to create a paginated page for each author's collection, since I don't know what the author's names will be. (The JSON data with the author's names and posts will be fetched from the web.) For example: // authorNames is an array of the author's names
authorNames.forEach((name) => {
eleventyConfig.addCollection(`postsBy${name}`, function() {
// create collection in here
})
}) So then I would have collections named ---
pagination:
data: collections.postsByAlice # <-- can't do this, I don't know the "postsByName" values
--- |
Beta Was this translation helpful? Give feedback.
-
You actually need to create a flattened chunked array of your posts for each author : [{
authorName: "bob",
pageNumber: 0,
totalPages: 2,
pageData: ['post-1','post-2']
},{
authorName: "bob",
pageNumber: 1,
totalPages: 2,
pageData: ['post-3']
},{
authorName: "alice",
pageNumber: 0,
totalPages: 2,
pageData: ['post-4','post-5']
},{
authorName: "alice",
pageNumber: 1,
totalPages: 2,
pageData: ['post-6']
}
...
] And then paginate that with something like: ---
pagination:
data: collections.authorsPosts
size: 1
alias: author
permalink: /authors/{{ author.authorName }}/{% if author.pageNumber %}{{ author.pageNumber + 1 }}/{% endif %}
---
<h1>Author: {{ author.authorName }}</h1>
<ul>
{% for post in author.pageData %}
<a href="{{post.url}}"><li>{{ post.data.title }}</li></a>
{% endfor %}
</ul>
<p>page:{{ author.pageNumber + 1}} of {{author.totalPages}}</p> If your data is coming from the web then you don't necessarily need to create a collection, just the right shaped JS array in a global data file. You can then paginate accross that. Without your exact data structure it's a bit difficult to replicate but here is a repo with the concept working with a collection of posts. You should be able to adapt this to your data structure. Shout if you need more help. |
Beta Was this translation helpful? Give feedback.
You actually need to create a flattened chunked array of your posts for each author :
And then paginate that with something like: