-
|
I don't know if I'm missing something fundamental or this is a quirk specific to shortcodes. When I call a module function for a shortcode, the value I had initially passed into the constructor seems to be undefined. This happens with normal shortcodes and Nunjucks async shortcodes. I've got a contrived example to illustrate. In a separate module I created this class test.shortcode.js: module.exports = class Square {
constructor(initialValue) {
this.initialValue = initialValue; // <-- area of interest
}
async doWork(anotherValue) {
return `${this.initialValue}:${anotherValue}`;
}
}Then in eleventy.js I am making a shortcode call. let TestShortCode = require('./_configs/test.shortcode.js');
let tsc = new TestShortCode('aaa'); //<--- initialValue
eleventyConfig.addNunjucksAsyncShortcode('mytest', tsc.doWork);Now when I use this shortcode in a post, I get the output: So the value passed in the constructor isn't available when the call gets made. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Interesting, this seems to work: -eleventyConfig.addNunjucksAsyncShortcode('mytest', tsc.doWork);
+eleventyConfig.addNunjucksAsyncShortcode('mytest', async (value) => tsc.doWork(value)); |
Beta Was this translation helpful? Give feedback.
Interesting, this seems to work: