Pagination with Jekyll and Foundation Sites 6
Posted on September 9, 2018 | Reading time 5 minutes- I share some code, to add styled pagination for Jekyll sites based on Foundation Sites 6
- The code runs on GitHub pages, so you don’t need to build your site locally/run jekyll on CI
Prerequisites
This post assumes, that you already have a site running. It’s also a good idea to run bundle update
, to get your dependencies up to date. I will utilize the Jekyll pagination plugin which is available on GitHub pages, so you don’t need to build your site locally.
Important: the plugin only works when your posts are listed on your index.html
page, this is a restriction of GitHub pages, which bundles jekyll-paginate in version 1.1.0 at the time of writing.
Configuration
In your _config.yml
, add the pagination plugin and configure it, for instance with the code below:
|
|
- The entry under
plugins
simply loads the plugin. If you get any errors when building your page, make sure yourgemfile
containsgem "github-pages", group: :jekyll_plugins
and rerunbundle install
paginate
sets the number of pages to display on one pagepaginate_path
specifies where the page files will be located. The first page will always beindex.html
, subsequent files will be in this case/posts/page1/
,/posts/page2/
,/posts/page3/
, and so on.
See the official documentation for more details about these options.
The paginator-element
Create the file _includes/paginator.html
. Files in this directory can be included via {% include paginator.html %}
, so you don’t need to have the same code twice, if you want to place the controls above and below your posts. The code for this file is as follows:
|
|
This code will create the control only, if there are actually multiple pages. The appropriate CSS classes are applied for the current page, as well as all the other elements. The edge case for the first page, which is just index.html
instead of /posts/page1
.
Important: Your paginate_path
needs to match href="/posts/page{{ counter }}"
on line 26.
Adjusting the index.html file
Your index.html
has most likely something like the following to display your posts:
|
|
This needs to be adjusted slightly, and the paginator.html
needs to be included:
|
|
Conclusion
The code in this post is in the public domain (see below), so feel free to reuse it. This pagination control is easy to implement on GitHub pages. If you need more advanced features, there is jekyll-paginate-v2 which is also backwards compatible. The plugin there unfortunately is not supported on GitHub pages, so you’ll need to set up continuous integration, for instance with Travis in order to use it.
If you reuse the code, I’d appreciate a ping back in the comments, but this is not required.
License for the paginate element in this post
|
|