Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access paginated context #280

Closed
edwardhorsford opened this issue Oct 19, 2018 · 8 comments
Closed

How to access paginated context #280

edwardhorsford opened this issue Oct 19, 2018 · 8 comments
Labels
documentation open-question Requires additional information before we can proceed.
Milestone

Comments

@edwardhorsford
Copy link
Contributor

edwardhorsford commented Oct 19, 2018

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.

@edwardhorsford
Copy link
Contributor Author

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.

@zachleat
Copy link
Member

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’s items?

@zachleat zachleat added the open-question Requires additional information before we can proceed. label Oct 30, 2019
@edwardhorsford
Copy link
Contributor Author

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?

@zachleat
Copy link
Member

zachleat commented Nov 1, 2019

Pagination chunks the data into pages. pagination.items is the current page’s 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’s 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).

@edwardhorsford
Copy link
Contributor Author

edwardhorsford commented Nov 1, 2019

@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.

@zachleat
Copy link
Member

zachleat commented Nov 2, 2019

Ah right on—thanks for clarifying. Regardless of whether you intended to or not, you’ve 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 zachleat added this to the Next Minor Version milestone Nov 2, 2019
@edwardhorsford
Copy link
Contributor Author

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

@zachleat
Copy link
Member

zachleat commented Nov 6, 2019

Docs added in 11ty/11ty-website@322c5b2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation open-question Requires additional information before we can proceed.
Projects
None yet
Development

No branches or pull requests

2 participants