-
Hey all, I was trying something and I'm not sure if it's actually impossible, or just my tinkering-skills that fall short. When I create a function in .eleventy.js with The idea I thought I'd write the notes on my website with a boolean Next step would be to trigger an action on The thing is, I don't know how to access the collection I create outside of a template. Is this data even reachable? I don't know.
But both these syndicate from an export, and it feels weird for me to first export data to a format, and then process that export. If possible, I'd rather take the data at the source (inside eleventy) and syndicate that! Thanks for any feedback on this! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I found a workaround, but not sure I like it. It involves manually [re]creating collections via /**
* @param {import("@11ty/eleventy/src/UserConfig")} eleventyConfig
* @returns {ReturnType<import("@11ty/eleventy/src/defaultConfig")>}
*/
module.exports = function (eleventyConfig) {
const collectionData = {};
const tags = ["blog"];
for (const t of tags) {
eleventyConfig.addCollection(t, function (collectionApi) {
collectionData[t] = collectionApi.getFilteredByTag(t);
return collectionData[t];
});
}
eleventyConfig.addCollection("docs", function (collectionApi) {
collectionData.docs = collectionApi.getFilteredByGlob("src/docs/*.njk");
return collectionData.docs;
});
eleventyConfig.on("eleventy.after", function (cfg) {
console.log(collectionData);
});
return {
dir: {
input: "src",
output: "www",
}
};
}; Where ./src/docs/page-2.njk looks like this masterpiece: ---
title: Page Two
---
<h1>{{ title }}</h1> And my blog posts directory data file look like this: module.exports = {
layout: "blog.njk",
tags: ["blog"],
eleventyComputed: {
title(data) {
return data.page.fileSlug;
},
},
}; So basically one auto-collection via In my case, it will return a big-ol object looking something like this: {
blog: [
{
template: [Template],
data: [Object],
page: [Object],
inputPath: './src/blog/post-1.md',
fileSlug: 'post-1',
filePathStem: '/blog/post-1',
date: 2023-09-08T04:17:28.266Z,
outputPath: 'www/blog/post-1/index.html',
url: '/blog/post-1/',
templateContent: [Getter/Setter],
content: [Getter]
},
{
template: [Template],
data: [Object],
page: [Object],
inputPath: './src/blog/post-10.md',
fileSlug: 'post-10',
filePathStem: '/blog/post-10',
date: 2023-09-08T04:17:28.266Z,
outputPath: 'www/blog/post-10/index.html',
url: '/blog/post-10/',
templateContent: [Getter/Setter],
content: [Getter]
},
...,
{
template: [Template],
data: [Object],
page: [Object],
inputPath: './src/blog/post-9.md',
fileSlug: 'post-9',
filePathStem: '/blog/post-9',
date: 2023-09-08T04:17:28.266Z,
outputPath: 'www/blog/post-9/index.html',
url: '/blog/post-9/',
templateContent: [Getter/Setter],
content: [Getter]
}
],
docs: [
{
template: [Template],
data: [Object],
page: [Object],
inputPath: './src/docs/page-1.njk',
fileSlug: 'page-1',
filePathStem: '/docs/page-1',
date: 2023-09-08T04:18:07.188Z,
outputPath: 'www/docs/page-1/index.html',
url: '/docs/page-1/',
templateContent: [Getter/Setter],
content: [Getter]
},
...
]
} |
Beta Was this translation helpful? Give feedback.
I found a workaround, but not sure I like it. It involves manually [re]creating collections via
.addCollection()
and caching the values in a global object which you can access ineleventy.after
event.