Skip to content

Commit

Permalink
Merge pull request #1698 from Jaza/page-if-no-data
Browse files Browse the repository at this point in the history
Pagination: add pageOnEmptyData option
  • Loading branch information
zachleat authored May 11, 2022
2 parents 7da411d + 045ca99 commit 2dc653a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Plugins/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,10 @@ class Pagination {
// TODO future improvement dea: use a light Template wrapper for paged template clones (PagedTemplate?)
// so that we don’t have the memory cost of the full template (and can reuse the parent
// template for some things)

for (
let pageNumber = 0, pageNumberStop = items.length;
pageNumber < pageNumberStop;
let pageNumber = 0;
pageNumber < items.length;
pageNumber++
) {
let cloned = this.template.clone();
Expand Down
40 changes: 40 additions & 0 deletions test/PaginationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,46 @@ test("No pagination", async (t) => {
t.is((await paging.getPageTemplates()).length, 0);
});

test("Empty paged data", async (t) => {
let eleventyConfig = new TemplateConfig();
let tmpl = getNewTemplate(
"./test/stubs/paged/paged-empty.njk",
"./test/stubs/",
"./dist",
null,
null,
eleventyConfig
);

let data = await tmpl.getData();
let paging = new Pagination(data, tmpl.config);
paging.setTemplate(tmpl);

t.is(paging.getPageCount(), 0);
t.is(paging.pagedItems.length, 0);
t.is((await paging.getPageTemplates()).length, 0);
});

test("Empty paged data with pageOnEmptyData enabled", async (t) => {
let eleventyConfig = new TemplateConfig();
let tmpl = getNewTemplate(
"./test/stubs/paged/paged-empty-pageonemptydata.njk",
"./test/stubs/",
"./dist",
null,
null,
eleventyConfig
);

let data = await tmpl.getData();
let paging = new Pagination(data, tmpl.config);
paging.setTemplate(tmpl);

t.is(paging.getPageCount(), 0);
t.is(paging.pagedItems.length, 0);
t.is((await paging.getPageTemplates()).length, 1);
});

test("Pagination enabled in frontmatter", async (t) => {
let eleventyConfig = new TemplateConfig();
let tmpl = getNewTemplate(
Expand Down
8 changes: 8 additions & 0 deletions test/stubs/paged/paged-empty-pageonemptydata.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
pagination:
data: items
size: 1
pageOnEmptyData: true
items: []
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>
7 changes: 7 additions & 0 deletions test/stubs/paged/paged-empty.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
pagination:
data: items
size: 1
items: []
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>

0 comments on commit 2dc653a

Please sign in to comment.