Trying to use pagination with 11ty.js template...not succeeding #3082
-
I am generating json files for the api that the 11ty.dev docs use for the CMS and Getting Started areas of the doc. They come from the 11tybundle.dev categories of the same name. I am currently using one template each for these two categories, but I am thinking of using pagination over an array of category names so that I don't need a separate template file for each category that I do this for. Here's what one of the templates looks like: // Output only those records from 11bundle.dev that are blog posts
// with the specified category
const _ = require("lodash");
const thisCategory = "Getting Started";
const sourceData = require("../_data/allrecords.json");
module.exports.data = function () {
return {
permalink: "/api/" + _.kebabCase(thisCategory) + ".json",
};
};
module.exports.render = function (data) {
const bundlePosts = sourceData;
function isCategory(item) {
return (
item["Type"] == "blog post" && item["Categories"].includes(thisCategory)
);
}
const sortedPosts = bundlePosts.filter(isCategory).sort((a, b) => {
return a.Date > b.Date ? -1 : 1;
});
return JSON.stringify(sortedPosts, null, 2);
}; Here's what I'm trying to do with a paginating template. The problem I can't seem to solve is generating the permalink. The permalink should be of the form /api/{category name}.json. Running with the following template results in a bit of a mess. Any guidance would be appreciated. Thanks! // Create json files for the listed set of categories
// Files are created in the /api folder off the root of the site
const _ = require("lodash");
const sourceData = require("../_data/allrecords.json");
module.exports.data = {
pagination: {
data: "categories",
size: 1,
alias: "category",
},
categories: ["Getting Started", "CMS"],
};
module.exports.render = function (data) {
const bundlePosts = sourceData;
function isCategory(item) {
return (
item["Type"] == "blog post" && item["Categories"].includes(data.category)
);
}
const sortedPosts = bundlePosts.filter(isCategory).sort((a, b) => {
return a.Date > b.Date ? -1 : 1;
});
return JSON.stringify(sortedPosts, null, 2);
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
With the help of an able-bodied helper on the Discord server, my problem has been solved. Here is the code in working form. I learned a lot. // Create json files for the listed set of categories
// Files are created in the /api folder off the root of the site
// The file name is the category name in kebab case
const _ = require("lodash");
const sourceData = require("../_data/allrecords.json");
class MyJsonFiles {
data() {
return {
pagination: {
data: "categories",
size: 1,
alias: "category",
},
// These are the categories that generate json files in the api directory
categories: ["Getting Started", "CMS"],
permalink: (data) => `/api/${_.kebabCase(data.pagination.items[0])}.json`,
};
}
render(data) {
const bundlePosts = sourceData;
function isCategory(item) {
return (
item["Type"] == "blog post" &&
item["Categories"].includes(data.pagination.items[0])
);
}
const sortedPosts = bundlePosts.filter(isCategory).sort((a, b) => {
return a.Date > b.Date ? -1 : 1;
});
return JSON.stringify(sortedPosts, null, 2);
}
}
module.exports = MyJsonFiles; |
Beta Was this translation helpful? Give feedback.
With the help of an able-bodied helper on the Discord server, my problem has been solved. Here is the code in working form. I learned a lot.