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

Get collection|length of paginated collection #29

Open
inktrap opened this issue Oct 12, 2017 · 7 comments
Open

Get collection|length of paginated collection #29

inktrap opened this issue Oct 12, 2017 · 7 comments

Comments

@inktrap
Copy link

inktrap commented Oct 12, 2017

I would like to get the length of the collection in a twig template.
If I have a collection of 11 pages:

and I 1) disable pagination or set the limit to 0, I can use this to get the length of a collection:

{% if collection | length > 10 %}
more than your collection of 10 pages.
{% endif %}

However, 2) with pagination enabled and a limit set to 10, this won't work, since the length of the collection is the current paginated collection.

So my question would be: can I (a) calculate the total number of pages somehow, (b) is the number stored in some variable which I have not found yet, or (c) should I implement a workaround (do pagination in twig), like in #13 ?

@rhukster
Copy link
Member

pagination in twig is not really the best way, i think actually this is something to look at in the Collection class itself, within the Grav core. I'm surprised there isn't a method already. I'll have to have a look.

@inktrap
Copy link
Author

inktrap commented Nov 16, 2017

@rhukster since I am actively using grav and would like to use pagination and get the total number of articles … I would like to ask if there has been any progress yet?! Don't want to stress, though.

@RosemaryOrchard
Copy link

I would also appreciate a way to get the size of a collection without hacking around.

@brickcamp
Copy link

Quite old issue but in case someone reads it searching for a solution:

I handle this issue by declaring a second collection (named "unpaginated") in the frontmatter like seen here and using it to access the full length like here. It's not beautiful but it delivers the results I need.

@webstationhq
Copy link

We recenrly ran into this issue. As of version 1.6.25, this is still not fully solved. If there are others who would like to have access to this via twig, I am contemplating writing something in order to use a twig statement like:

{% page.collection | raw_length %}

To provide the actually length of self.siblings prior to pagination.

Or, has this been solved and we have missed it? If so, any help is appreciated. We are fairly new to Grav (and LOVE it).

@webstationhq
Copy link

Found another solution that seems to work well and wanted to share here

Background:

We have a page using the blog twig template, several subpages (posts) using the item twig template. We wanted to use pagination with a snippet of text that says "Showing page X of Y".
Since the pagination limit was set at 3, we could not get this to print "Showing 3 of 4" since

{{ page.collection | length }} 

only returns the current page. To solve it, we added this to the front matter:

content:
items: '@self.children'
    limit: '3'
    order:
        by: date
        dir: asc
    pagination: true
unpaginated:
    items: @self.children
    filter:
        published: true

and then used this code in the twig of the parent page using the blog template in the pagination.html.twig file:

<li class="page-item disabled">
       {# See issue https://github.com/getgrav/grav-plugin-pagination/issues/29 #}
       {% set fullsize = page.header.unpaginated ? page.collection('unpaginated') : page.collection %}
        <span class="page-link"  aria-hidden="true">Showing {{ page.collection | length }} of {{ 
fullsize|length }} properties</span>
        <span class="sr-only">Previous</span>

Here is the output:

Screen Shot 2020-05-23 at 12 30 36 PM

@mbirth
Copy link

mbirth commented Jul 19, 2021

It's 2021 now. Is there really no better solution as of yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants