-
-
Notifications
You must be signed in to change notification settings - Fork 496
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
Expose nunjucks rendering context to shortcode #1596
Comments
I believe this is a duplicate of #1584, can you confirm? |
|
@zachleat If Longer explanation: My component shortcode calls |
@zachleat I upgraded to the latest beta to try this out and it's not solving the issue raised here.
I think what I'm looking for here is access to the |
Circling back here, I wonder if you can make use of the Render plugin, which should give you access to all of your top level configuration stuff for free? https://www.11ty.dev/docs/plugins/render/ Alternatively, I’ve also been working on a e.g. eleventy/test/TemplateRenderPluginTest.js Line 252 in f05caea
|
@zachleat This is very cool and did not realize it was a thing. In my case, I'm rendering programmatically — having access to the render API is what should do the trick. Here's a quick look at what's going on: Dynamically-Generated ShortcodesI have a shortcode like this: {% button url = "/", label = "Go Home" %} But this is generated dynamically be reading files in a Current IssueThe system works well except that I then have to use transformers to render shortcodes used within the dynamic components. For example, say I have a card that renders a button, where the button is also a dynamic shortcode. That requires the card to have a transformer where it does something like this: module.exports = (input) => {
let output = {}
if (input.button) output.button = Component.render(input.button)
return output
} And then in the template, I render the string, rather than a shortcode. {{ button | safe }} |
I do believe this is possible with the existing RenderPlugin, something like this: const { EleventyRenderPlugin } = require("@11ty/eleventy");
const compileFile = EleventyRenderPlugin.File;
const compileString = EleventyRenderPlugin.String; |
Thanks for posting about it here @zachleat. Even though I couldn't find any docs, ended up using In case you're wondering, usage would look something like this: {% for section in city.sections %}
{% case section.type %}
{% when 'content' %}
<article>
{% renderFromString section.content %}
</article>
{% endcase %}
{% endfor %} And here's the rough version of my shortcode: function addRenderFromString(eleventyConfig) {
const rm = new RenderManager();
eleventyConfig.on('eleventy.config', (cfg) => {
rm.templateConfig = cfg;
});
eleventyConfig.addAsyncShortcode('renderFromString', async function(content) {
const fn = await rm.compile(content, "liquid,md");
return fn(this.ctx.getAll());
});
} I went with overwriting templateConfig, to not have to duplicate paths and general configuration 🤔 If there's a better solution, would be keen to learn about it 🤓 |
|
Note that in 3.0.0 onwards, |
Is your feature request related to a problem? Please describe.
I've build a server-side component system using Nunjucks. There is a method that dynamically generates shortcodes based on components within some given directory.
Then instead of doing this:
I can do this:
That works well, but only when on level deep. I'm using the nunjucks library directly and so I don't have Eleventy's nunjucks rendering context. That makes it difficult to nest components or to share other customizations I've made elsewhere.
Describe the solution you'd like
I'd love to expose the nunjucks instance to the shortcode.
Describe alternatives you've considered
I've begun playing around with other component libraries like Vue and React. But I really like the simplicity of a system with server-side templates like Nunjucks.
Additional context
I would be happy to work on the solution. I think I have an idea where to start, too. But curious if this is a good idea/direction for the project?
The text was updated successfully, but these errors were encountered: