Goal
I’m building a book as a website, and want each page to have buttons that point to the previous page & the next page so readers don’t have to use the sidebar navigation to get to every page. (I’m using Tom Johnson’s Jekyll Documentation theme to for this project.)
Description of problem
Here are the relative elements of my project:
Mapping
The logic that builds the sidebar pulls from mydoc_sidebar.yml
, which looks something like this:
entries:
- title: sidebar
product: Contents
version:
folders:
- title: Frontmatter
output: web, pdf
folderitems:
- title: Title
url: /index.html
output: web, pdf
type: homepage
weight: 1.0
- title: Table of Contents
url: /contents.html
output: web, pdf
weight: 1.0
- title: Part 1
output: web, pdf
folderitems:
- title: Poem 1
url: /poem1.html
output: web, pdf
weight: 1.0
# mapping for other poems here
- title: Part 2
output: web, pdf
folderitems:
- title: Poem 10
url: /poem10.html
output: web, pdf
weight: 1.0
# mapping for other poems here
I’m not using the weight
variable right now, but it’s part of every item in case I can use that to create the Previous/Next buttons.
Layouts
The front- & backmatter files use the page
layout, and the poems use a custom layout based on default
—both of which reference my next.html
include that I can’t figure out.
Poems are also a Collection (in case I can leverage that).
Steps to reproduce
Here’s the logic in next.html
:
{% for item in site.data.sidebars.mydoc_sidebar.entries %}
{% for folder in item.folders %}
{% for folderitem in folder.folderitems %}
{% if folderitem.url == page.url %}
{% assign folderitem.url = thisPage %}
{% assign thisPage[forloop.index] = "0" %}
{% assign prevPage = thisPage | minus: "1" %}
{% assign nextPage = thisPage | plus: "1" %}
<strong>This page index: {{ forloop.index }}, {{ page.url | remove: "/" }}</strong>
<br>
<strong>Prev page index: {{ prevPage }}, {{ prevPage.title }}, {{ prevPage.url | relative_url }}</strong>
<br>
<strong>Next page index: {{ nextPage }}, {{ nextPage.title }}, {{ nextPage.url | relative_url }}</strong>
{% break %}
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
<div class="container">
<div class="row">
<div class="col-sm-4">
<i class="fa fa-arrow-left fa-lg"></i> <a href="{{ prevPage.url }}"><span class="label label-primary">Previous</span></a>
</div>
<div class="col-sm-4">
<a href="{{ nextPage.url }}"><span class="label label-primary">Next</span></a> <i class="fa fa-arrow-right fa-lg"></i>
</div>
</div>
</div>
Everything in the <strong>
tags is just some logic I found to help identify my problem, and won’t be in the final product.
Expected result
I expect next.html
to spit out buttons that link to the previous & next pages as defined in mydoc_sidebar.html
. Would look something like this:
Actual result
The buttons are output, but when I hover over the links, the URL for both Previous & Next is always the URL of the current page.
Here’s what’s output from those <strong>
tags:
This page index: 6, poem6.html
Prev page index: -1, ,
Next page index: 1, ,
Based on that, I can see that my logic is wrong. The prevPage
& nextPage
variables are not accessing those pages in the YAML mapping, hence the blanks after “-1” & “1” where a title & URL should be.
Notes
- I know the Jekyll tutorials use similar logic to create Previous/Next buttons, but even after looking at the source files, I can’t figure out what I’m doing wrong.
- I can’t point you to my Git repo. I haven’t initialized it yet because I hate Git (even though I do use it daily in my professional life), and don’t want to deal with it until I’m ready to build the website.
This feels like it should be pretty simple, but I guess I don’t have the same talent for abstraction that developers have.