-
Notifications
You must be signed in to change notification settings - Fork 0
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
Inject ctx into addFilter #72
Comments
Monkeypatching |
- for (const K of CONTEXT_DATA_KEYS) {
- env.utils._11tyCtx[K] = newContext[K];
- }
+ env.utils._11tyCtx = newContext; simply doing this seems to work |
Hi— feature requests should use the issue template when creating them. No need to open a new issue but please use the template going forward. It helps me get the right information related to your issue so I can triage appropriately. I'm curious to get more information about your use case. If you're using this plugin, importing Second, I'm confused why you would need an interpolate filter that re-runs Vento since Vento provides support for If your use case is to run a template passed as a string, like so: {{ '{{ 2 + 4 }}' | interpolate }} why not just do {{ 2 + 4 }} If you're trying to render Vento from another template, you can use Eleventy's render plugin to render other template languages from inside a template. The documentation you referenced states that Lastly for a bit of background, setting In other words, since EDIT: I just noticed the linked issue in the Vento repository. I'll take a look at that and add additional comments if I have any to this issue. Stand by! |
@boehs Ah, ok, I see what you're trying to do. It looks like you got there with a custom vento plugin loaded into the To improve on that further, what I could do on my end is assign {
"viewport": "width=device-width,initial-scale=1",
"theme-color": "{{color}}",
"author": "{{config.author.name}}",
"generator": "{{ eleventy.generator }}" // <-- you can use `eleventy.generator` here
} Instead of relying on a custom plugin your declaration could be something like this: eleventyConfig.addFilter('vento', function (code) {
const file = `memory:${await hash(code)}.vto`;
let res = await this.ctx.env.runString(code, this.ctx.data, file);
return res.content;
}); and then your template would be: {{ for name,content of metaTags }}
<meta name="{{name}}" content="{{ content |> vento |> escape}}">
{{ /for }} Then you wouldn't have to import ventojs or initialize a separate environment. Thoughts? |
As an addendum, is there a reason you don't want to have a In this particular case it doesn't seem unreasonable to maintain a file that consists of: <meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="theme-color" content="{{ color }}">
<meta name="author" content="{{ config.author.name }}">
<meta name="generator" content="{{ eleventy.generator }}"> and then in ...
<!-- page title -->
<title>...</title>
<!-- meta tags -->
{{ include 'meta.vto' }}
... That way you have proper syntax highlighting, can support multiple attributes on meta tags (future-proofing), and eliminate extra load and compile steps in memory. |
I took a deeper look into this idea and its definitely something I could implement. However, there's a big caveat to note, which is Do note also 11ty/eleventy#2844, which is tracking adding the entire data cascade to filters and shortcodes. If implemented this plugin will follow suit with whatever Eleventy does to maintain feature parity. |
Hi, thanks for the detailed response
Yes, that would be ideal!
Good call!
At boehs.org enterprises everything is absurdly overengineered :-) https://github.com/boehs/site/blob/df888b1d877615f5313153022841cdf7af85e90f/.eleventy.js#L228-L269
Good point on the instability, I do think it would be nice to have with that note nevertheless! I'm sure there's other reasons why you'd want to access the cascade in a filter
Eagerly await! Thank you for the amazing response and this great library |
Shipped in 4.1.0!
Disclosure: I wouldn't consider shipping this feature if it wasn't something Vento included as part of its own design. Since Biggest reason against is that I'd highly reccomend avoiding excess compilation via template engines in Eleventy. The docs makes a point of recomending against template strings (see end of that section) when computing data, as using them incurs a performance overhead. You can achieve a much more elegant result that's template engine independent by converting your JSON data files to JS modules that export objects. Then import the objects from other data file modules to create data dependence, since not all template engines will expose |
For nunjucks, this works:
but, ctx is undefined for us:
according to the Eleventy docs:
is there some way for this library to inject ctx here? I believe liquid also does it somehow, but with a different variable name
The text was updated successfully, but these errors were encountered: