Skip to content
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

eleventyConfig.augmentFunctionContext to add this.eleventy and this.page to the context of any function (but mostly for filters/shortcodes) #3355

Closed
zachleat opened this issue Jul 8, 2024 · 8 comments

Comments

@zachleat
Copy link
Member

zachleat commented Jul 8, 2024

Sample usage here creating a Nunjucks shortcode from scratch:

eleventy/test/EleventyTest.js

Lines 1435 to 1445 in 26d26b7

let fn = eleventyConfig.augmentFunctionContext(
eleventyConfig.getShortcode("customized"),
{
source: context.ctx,
// lazy: false,
// getter: (key, context) => context?.[key];
// overwrite: true,
}
);
return fn(argString);

Related to #3310 and noelforte/eleventy-plugin-vento#9

@zachleat zachleat added this to the Eleventy 3.0.0 milestone Jul 8, 2024
@zachleat
Copy link
Member Author

zachleat commented Jul 8, 2024

Fixed by 26d26b7

@pauleveritt
Copy link

This might be a really useful thing for my TSX stuff.

@zachleat
Copy link
Member Author

zachleat commented Jul 9, 2024

@pauleveritt Yeah absolutely!

@zachleat
Copy link
Member Author

@noelforte
Copy link

noelforte commented Aug 28, 2024

Revisiting this, with the addition of an exports field in eleventy's package.json, it's no longer possible to access any utilites by their direct filepath. For instance, this throws an error:

import { augmentKeys } from '@11ty/eleventy/src/Engines/Util/ContextAugmenter.js';

Does it make sense to mark ContextAugmenter.js's exports as public so they can be consumed elsewhere outside of eleventyConfig?


As an aside, I just came across #3422 which makes the suggestion to do away with usage of this in JavaScript templates. It got me curious if there's a future for Eleventy to maintain some sort of global object containing useful data (like page and eleventy) that could be imported project-wide, which would alleviate my struggles with this and my template syntax plugin for Vento.

Removal of dependence on this is a big breaking change, so definitely a topic for larger discussion. Interesting to consider, though!

@mayank99
Copy link
Contributor

mayank99 commented Sep 8, 2024

I think this might solve some of the problems I was facing which led me to open #3422, but I don't see any docs for augmentFunctionContext. Maybe this issue needs the needs-documentation Documentation for this issue/feature is pending! label?

@zachleat zachleat added the needs-documentation Documentation for this issue/feature is pending! label Sep 20, 2024
zachleat added a commit to 11ty/11ty-website that referenced this issue Sep 27, 2024
@zachleat zachleat removed the needs-documentation Documentation for this issue/feature is pending! label Sep 27, 2024
@zachleat
Copy link
Member Author

Temporary docs preview deploying here https://11ty-website-git-v3-11ty.vercel.app/docs/languages/custom/#compile

@noelforte
Copy link

Thanks for this @zachleat! After getting more educated on the implementation for my use case in eleventy-plugin-vento. I realized it would be simpler to precompile templates with a direct call to the shortcode instead of an indirect call.

I did stumble upon one thing though, I see augmentObject and augmentKeys are exported from src/Engines/Util/ContextAugmenter.js, but trying to import them throws an error. Filed in #3460, which might also help clean up things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants