Eleventy: How to access paginated context

Created on 19 Oct 2018  路  8Comments  路  Source: 11ty/eleventy

Forgive me if this is a bit confusing.

I'm working on some paginated pages - essentially tag pages. For each, I'd like to be able to get the first 'item' in that page's collection. The catch is I'm doing this from a parent template that doesn't know the alias used by the paginated page - so can't can't just use the alias.

The data available in data.pagination ({ data: 'collections.byFoo', size: 1, alias: 'foo' }) doesn't give me enough to work on to get the latest item.

Ultimately I want to be able to call collections[pagination][key][0] to get the latest item - I can derive pagination, but it the default data doesn't tell me what foo is a the time it's called.

Is it possible actual value of foo? I know it's available by calling foo - but I'm using the alias feature quite a lot.

The best I've thought of so far is to use this in the frontmatter:

---
renderData:
 paginationKey: {{foo}}
---

This feels rather awkward.


Related, it would be helpful if more data about the current page were available to that page. Could the contents of the page object mirror the contents of collection.foo.data? Or am I missing something?

Right now, to work out if I'm on a paginated page I'm iterating through all pages in collections.all, comparing fileSlugs to see if I have a match - then looking in collection.foo.data.pagination.

documentation open-question

Most helpful comment

Ah right on鈥攖hanks for clarifying. Regardless of whether you intended to or not, you鈥檝e exposed another limitation with pagination that I will fix 馃榾.

pagination.alias and pagination.pages (the full chunked data set) will be available in the next version. Docs pending.

All 8 comments

A bump from me - it would be _super_ useful if the pagination object included on a paginated page included the 'key' used to generate it. It gives the collection itself, but not the iterator for that item.

If I understand you correctly, what you are trying to do should be achievable with the current pagination object, specifically pagination.items, pagination.pageNumber, pagination.data?

Are you saying you want the first/next/previous/last page鈥檚 items?

Hi @zachleat.

It's been a while since I've looked at this. Looking at the pagination object, I think what I want is pagination.items[0] - essentially the value of the alias. My parent template doesn't know the aliasses my templates use, so the only way to get that value was with renderdata. But pagination.items[0] seems to be it - has it always been there?

Pagination chunks the data into pages. pagination.items is the current page鈥檚 chunk of data. If you want the first item in the current page chunk, pagination.items[0] is there, yeah. But if you want the first item in the paginated set, therein lies the limitation I think!

For example, if I have a collection of blog posts and want to display links to the next/previous blog posts in the collection, there鈥檚 no easy way to do this via pagination. I mean, you easily get access to the URL for the previous and next page, but not anything about the actual items (e.g. if you want to show the Blog Post title in the link). You would need to iterate over the collection, which is more work than you should have to do (imo).

@zachleat I'm not sure we're on the same page, but it might be a moot point.

If in my paginated page I have this in my frontmatter:

alias: tag

On a given page, the value of {{tag}} might be Fruit.

In a parent template which doesn't know the alias I'm using in my source page template (where pagination is defined), I'm trying to get the value of tag at that time. I can't use {{tag}} as my parent template doesn't know anything about the template's aliases. It does look like pagination.items[0] might equate to what I want.


FWIW, I have a filter* I use to look up post data via url:

exports.filterPostsByUrl = function(items, slug, exactMatch=false) {

  var output = items.filter( item => {
    if (exactMatch==false && (typeof item.url === 'string' || item.url instanceof String)) return item.url.includes(slug)
    if (exactMatch==true && (typeof item.url === 'string' || item.url instanceof String)) return item.url == slug
    else return false   
  })

  // Return false if no items found
  return (output && output.length > 0) ? output : false
}

*There's a good chance I copied this from someone else.

Ah right on鈥攖hanks for clarifying. Regardless of whether you intended to or not, you鈥檝e exposed another limitation with pagination that I will fix 馃榾.

pagination.alias and pagination.pages (the full chunked data set) will be available in the next version. Docs pending.

@zachleat Awesome! Yes pagination.alias sounds like _exactly_ what I was after.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matt-auckland picture matt-auckland  路  3Comments

ndaidong picture ndaidong  路  4Comments

kaloja picture kaloja  路  3Comments

veleek picture veleek  路  3Comments

aaronstezycki picture aaronstezycki  路  3Comments