Allow Render plugin to access global data via $data
#4107
Replies: 2 comments
|
I think this is a good change! 🎉 Adding some more context as to why this issue came about. My original issueGlobal data is not available when trying to access it in a I'm attempting to render a {% renderTemplate "webc" %}
<greetings></greetings>
{% endrenderTemplate %}In the component file, I want to access some global data in a <ul>
<li webc:for="(greeting, index) of greetings" :id="greeting.id">
<span @text="index"></span>
<span @text="greeting.text"></span>
</li>
</ul>This renders {% renderTemplate "webc", greetings %}
<greetings></greetings>
{% endrenderTemplate %}This data can then be accessed on the component: <li webc:for="greeting of $data._"></li>If this index file were a <li webc:for="(greeting, index) of $data.greetings">Proposed workaroundAfter some discussion, Zach had provided this workaround for nunjucks: const { EleventyRenderPlugin } = require("@11ty/eleventy");
const pluginWebc = require("@11ty/eleventy-plugin-webc");
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(EleventyRenderPlugin);
eleventyConfig.addPlugin(pluginWebc);
eleventyConfig.addPairedNunjucksAsyncShortcode("renderWebC", async function(content) {
return eleventyConfig.javascriptFunctions.renderTemplate.call(this, content, "webc", this.ctx);
});
};and then in nunjucks template: ---
globalData: hi
---
{% renderWebC %}
<test :arg="globalData"></test>
{% endrenderWebC %}TestingFollowing this workaround, I've updated my test repo's eleventy.config and added a workaround page. I can confirm that global data can now be passed into an argument like this: {% renderWebC %}
<greetings :greetings="greetings"></greetings>
{% endrenderWebC %} |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Render plugin has scoped data: https://www.11ty.dev/docs/plugins/render/#pass-in-data
However, I’d like to add access to global data for all supported template languages for the render plugin (Nunjucks, Liquid, 11ty.js), perhaps re-using the
$dataconvention from WebC https://github.com/11ty/webc/releases/tag/v0.11.0All reactions