-
-
Notifications
You must be signed in to change notification settings - Fork 498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global import of macros fails in Eleventy 3.0.0-alpha.14 #3345
Comments
Somewhat related to #188—though this request is limited to collection entries I think! This seems to work for me: eleventyConfig.addCollection("userCollection", async function (collection) {
let c = collection.getFilteredByTag("posts");
for(let item of c) {
const frontMatter = await item.template.read();
frontMatter.content = `lol\n${frontMatter.content}`;
item.template.frontMatter = frontMatter;
}
return c;
}); |
I reckon doing it via an (essentially unfiltered) collection is just a way to apply a template fragment to every page. It does unnecessarily "pollute" the collections with this dummy collection. Additionally, this collection approach does have the downside that these macros still aren't automatically available within layouts or includes. Therefore, I think I'd ultimately personally prefer it if there was a dedicated feature to define global macros such that they are available on every page, include, and layout, without the need for a dummy collection like this. Thanks for the snippet! I tried a very similar approach, but I didn't get far; I've set up a demo repo: https://github.com/groenroos/eleventy-global-macro Each subfolder has the same demo site on different versions of Eleventy, with your
This is the same behaviour that I get in my production repo; I initially thought it was a specific issue with using this technique in Markdown files (as my prod repo fell over on a Markdown page first), but this demo repo seems to fail with the Nunjucks template, too. |
Rather than hack around this one, I just added another API here to do this officially: I tested this with success in your repro repo, thanks! export default function(eleventyConfig) {
eleventyConfig.addPreprocessor("macro-inject", ".njk,.md", (data, content) => {
return `{%- from "component.njk" import component with context -%}\n` + content;
});
} |
Shipping with 3.0.0-alpha.17 |
That looks fantastic! Thank you, can't wait to try it out! 🥳 Does this method inject it into layouts and templates too, or just posts? 👀 |
Any template files—not layouts. |
Can confirm that this is resolved in Eleventy 3.0.0-alpha.17 as per the |
Operating system
macOS Sonoma 14.4.1
Eleventy
3.0.0-alpha.14
Describe the bug
Using the technique described in this comment to load common macros globally for each Nunjucks and Markdown template. It works without errors in Eleventy 2.x.
However, on Eleventy
3.0.0-alpha.14
, this technique quits the build with the following error:My understanding of the
TemplateContent
class (or other Eleventy internals) is limited, but I attempted to adapt it to use the advertisedread()
method:However, this does not appear to make the components available in Markdown files. As a test, I deleted the few Markdown pages that require components, and it then fails by confusingly claiming the collection is already defined (regardless of what the collection is named):
Reproduction steps
3.0.0-alpha.14
Expected behavior
Either the build should succeed, or an alternative pattern should be established for loading template macros globally such that they are available in every template
Reproduction URL
No response
Screenshots
No response
The text was updated successfully, but these errors were encountered: