Pagination
Turning one document into several pages from its front matter.
Add pagination to a document's front matter to turn it into several pages.
| Key | Default | Meaning |
|---|---|---|
data | required | A path starting with collections., for example collections.tags or collections.global.jokes.jokes, or an inline list. A path that does not exist fails the build. If it points to a struct, its keys are paged, sorted, and with size: 1 a key's value, when it is a struct, is merged into that page's prc. |
size | 1 | Items per page. |
alias | pagedData | Name under which each page's items appear in prc. |
Each page gets its items in prc[ alias ] and navigation in prc.paginationInfo:
paginationInfo.pageNumber, pageSize, totalPages, totalItems
paginationInfo.href.current, previous, next, first, last ("" when there is none)
paginationInfo.pages [ { number, href, current } ]
Page URLs come from the document's permalink, or from the URL it would have without one:
{{page}}or{{pageNumber}}in the permalink is replaced by the page number on every page, including page 1.{{<alias>}}is replaced by the slugified item. This needssize: 1and simple values.- Otherwise page 1 is the permalink itself and page n is
<permalink>page/<n>/. The permalink must end in/(or/index.html) when there is more than one page.
Every href points at a file the build writes.
A list of jokes from _data/jokes.json, five per page, at /jokes/, /jokes/page/2/, and so on:
html<!---
title: Jokes
permalink: /jokes/
pagination:
data: collections.global.jokes.jokes
size: 5
--->
<bx:output>
<bx:loop from="1" to="#prc.pagedData.len()#" index="i">
<p>#prc.pagedData[i]#</p>
</bx:loop>
<bx:if prc.paginationInfo.href.previous.len()>
<a href="#prc.paginationInfo.href.previous#">Previous</a>
</bx:if>
<bx:if prc.paginationInfo.href.next.len()>
<a href="#prc.paginationInfo.href.next#">Next</a>
</bx:if>
</bx:output>
One page per tag at /tag/<slugified tag>/:
html<!---
title: Tags
permalink: /tag/{{tag}}/
pagination:
alias: tag
data: collections.tags
--->
<bx:output>
<h4>Posts tagged as #prc.tag#</h4>
<bx:loop array="#collections.byTag[ slugify( prc.tag ) ]#" item="post">
<p><a href="#post.permalink#">#post.title#</a></p>
</bx:loop>
</bx:output>
Link to a tag page with the same slug: <a href="#relUrl( '/tag/' & slugify( tag ) & '/' )#">#tag#</a>.