Eleventy: Starting pagination count at 1 and not 0

Created on 13 Jun 2019  ·  4Comments  ·  Source: 11ty/eleventy

I've noticed that while paginating over a collection the count starts at 0. Is this normal?

If so, is there a way of starting the count at 1? I consider the top level page to be page 1, then you click next and view page 2.

Example folder and url structure:

/blog
/blog/2
/blog/3
/blog/4

This would mean you could display the pagination like below and your url structure would match - Github pagination follows the same pattern:

The user is viewing /blog (Page 1).

Pagination is the following:
Previous (disabled)
1 (active)
2 (clickable)
3 (clickable)
4 (clickable)
Next (clickable)

Whats' the best way to achieve this?
Should this behaviour be the default implementation?

education

Most helpful comment

There is an example of this on the docs (almost!)

https://www.11ty.io/docs/pagination/#remapping-with-permalinks

permalink: blog/{{ pagination.pageNumber + 1 }}/index.html
---

You can also use template logic here to get the full working example:

permalink: blog/{% if pagination.pageNumber > 0 %}{{ pagination.pageNumber + 1 }}/{% endif %}index.html
---

All 4 comments

I can imagine that it might be because „Arrays start with 0” (a heated debate among computer scientists).

There is an example of this on the docs (almost!)

https://www.11ty.io/docs/pagination/#remapping-with-permalinks

permalink: blog/{{ pagination.pageNumber + 1 }}/index.html
---

You can also use template logic here to get the full working example:

permalink: blog/{% if pagination.pageNumber > 0 %}{{ pagination.pageNumber + 1 }}/{% endif %}index.html
---

Added a few more docs for this too https://www.11ty.io/docs/pagination/#remapping-with-permalinks

Amazing! Thanks for your help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DirtyF picture DirtyF  ·  3Comments

zachleat picture zachleat  ·  3Comments

zachleat picture zachleat  ·  3Comments

nilsmielke picture nilsmielke  ·  4Comments

aaronstezycki picture aaronstezycki  ·  3Comments