How to create a pagination #303
-
I can't find ways to create pagination on my blog. I know about the pagination plugin, which preinstalls with lume. I must show pagination on the main or // index.njk
---
layout: layouts/base.njk
menu:
visible: true
title: Home
order: 0
---
{% set postslist = search.pages("type=article", "date=desc") %}
{% for post in postslist %}
{% include "layouts/post.njk" %}
{% else %}
<h2> This would display if the 'item' collection were empty </h2>
{% endfor %}
{# paginateation for website #}
{% for page in paginate( postslist , { size: 10 }) %}
<a href="{{page.url}}">{{ page.pagination.page }}</a> <br/>
<h1>{{page.url}} </h1>
{% endfor %} Also, the following code is not converting my pagination to // index.njk
{# paginate #}
{% for page in paginate( postslist , { size: 10,url: (n) => `posts/page/${n}/` }) %}
<a href="{{page.url}}">{{ page.pagination.page }}</a> <br/>
<h1>{{page.url}} </h1>
{% endfor %} The error looks like Caused by Template render error: (/home/rajdeepsingh/working/lume-project/index.njk) [Line 16, Column 39]
parseAggregate: expected comma after expression
at Module._prettifyError (https://deno.land/x/nunjucks@3.2.3/src/lib.js:32:11)
at Template.render (https://deno.land/x/nunjucks@3.2.3/src/environment.js:468:23)
at (https://deno.land/x/lume@v1.12.1/plugins/nunjucks.ts:88:16)
at NunjucksEngine.render (https://deno.land/x/lume@v1.12.1/plugins/nunjucks.ts:87:12)
at Renderer.render (https://deno.land/x/lume@v1.12.1/core/renderer.ts:178:32)
at Renderer.#renderPage (https://deno.land/x/lume@v1.12.1/core/renderer.ts:308:23)
at (https://deno.land/x/lume@v1.12.1/core/renderer.ts:124:51)
at concurrent (https://deno.land/x/lume@v1.12.1/core/utils.ts:162:33)
at async Renderer.renderPages (https://deno.land/x/lume@v1.12.1/core/renderer.ts:115:7) I create dynamic pages with // pages.tmpl.js
export const layout = "layouts/base.njk";
export default function* ({ search, paginate }) {
const posts = search.pages("type=article", "date=desc");
const options = {
url: (n) => `/pages/${n}/`,
size: 10,
};
for (const page of paginate(posts, options)) {
yield page;
}
} But I can't find the ways to join the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
To do that, you need to understand two concepts:
The idea is to generate a page for each group returned by the export const layout = "layouts/base.njk";
export default function* ({ search, paginate }) {
const posts = search.pages("type=article", "date=desc");
const options = {
// The page 1 is the homepage, set "/" as url
url: (n) => n === 1 ? "/" : `/pages/${n}/`,
size: 10,
};
// Yield the pages, but the index needs a different layout
for (const page of paginate(posts, options)) {
if (page.pagination.page === 1) {
page.layout = "layouts/index.njk"; // The first page has a different layout
}
yield page;
}
} Now you only need have two layouts, one for the home page and other for the rest. In the layouts you can iterate with the |
Beta Was this translation helpful? Give feedback.
To do that, you need to understand two concepts:
The i…