How to generate dynamic pages from other pages and shared data? #423
-
Maybe related to #303 (comment) I have a site (which I'm currently trying to convert to Lume) with lots of subpages. E.g. Folder structure
I want to collect all these subpages and use it as Shared data so it can be reused on other sites as well. I also want to create other subsites from this data dynamically e.g. site items/id/compare site activations/id/compare // comparePages.tmpl.js
import lume from "lume/mod.ts";
export default function* () {
const languages = ["de", "en"];
const site = lume();
// this list should be get from the Shared data
const compareProducts = site.data.collections.filter(...); // [{ p_id: 'ACT006/P01' }, { p_id: 'ACT006/P02' }, { p_id: 'ACT006/P03' }];
for (const lang of languages) {
for (const product of compareProducts) {
const a_p_id = product.p_id.split("/");
const a_id = a_p_id[0];
const p_num = a_p_id[1];
yield {
title: `Compare Test Site`,
layout: "test-compare.pug",
content: "",
lang: lang,
url: `/${lang}/activations/items/${a_id}/${p_num}/compare.html`,
};
}
}
} but I don't know how to reuse Shared data in I also tried using the search function but without success because it then looks as if shared data is not available in the templates. export default function* ({ search }) {
const languages = ["de", "en"];
const compareProducts = search.pages("type=product")
...
} In order to transfer the data to Shared Data, I currently created a plugin, but I don't know if this is the correct approach /**
site.use(collections({
activations: {
pattern: ["/activations/items/"],
sortBy: "a_date"
},
products: {
pattern: ["/products/items/"],
sortBy: "p_date"
},
}));
*/
export function collections(options: CollectionsOptions) {
return (site: Site) => {
const data: Collections = {};
Object.keys(options).forEach((c) => {
data[c] = [];
});
site.preprocess([".md", ".json"], (page) => {
const path = page.src.path;
Object.keys(options).forEach((c) => {
const option = options[c];
const paterns = option.pattern.map((p) => p);
paterns.forEach((p) => {
const isPath = path.includes(p);
if (isPath) {
const newData = Object.assign({}, page.data);
const hasPage = data[c].find((p) => p.url === newData.url);
if (!hasPage) {
data[c].push(newData);
}
}
});
});
});
site.data("collections", data);
};
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Shared data is merged with the page data and passed to the first argument of the default function: export default function* (data) {
const { search } = data;
const compareProducts = search.pages("type=product");
// Create pages to compare the products
for (const page1 of compareProducts) {
for (const page2 of compareProducts) {
if (page1 !== page2) {
yield {
title: `Comparing ${page1.data.title} vs ${page2.data.title}`,
layout: "compare-page.njk",
page1,
page2,
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
The pages generated dynamically don't inherit the contextual data.
You need to include the desired data in the yielded object. For example: