-
I can use a filter in a shortcode if the shortcode function is defined in .eleventyConfig like this: const getFoo = require('./filters/get-foo.js');
module.exports = (eleventyConfig) => {
eleventyConfig.addFilter("getFoo", getFoo);
eleventyConfig.addShortcode("table", function(phrase) {
return `<table>
<!-- map phrase to words; map each word to letter -->
${eleventyConfig.getFilter("getFoo")(letter)}
</table>`;
});
} What if the shortcode is in a separate file? const getFoo = require('./filters/get-foo.js');
const table = require('./shortcodes/table.js');
module.exports = (eleventyConfig) => {
eleventyConfig.addFilter("getFoo", getFoo);
eleventyConfig.addFilter("table", table);
} With the same code from the first example, but moved to |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
I believe you'd need to modify ./shortcodes/table.js to accept a function so you could pass the |
Beta Was this translation helpful? Give feedback.
-
I'm afraid that went a bit over my head. How do I write a function to accept a function? I can add a second parameter to table.js: module.exports = function(phrase, eleventyConfig) {
return `<!-- table markup -->`
} but how do I pass the context from .eleventy.js? |
Beta Was this translation helpful? Give feedback.
-
Sorry if I'm being daft, but I can't get this to work. Note that my table.js already has a parameter, Maybe it would help to see how your shortcode gets used. In my setup, I have a frontmatter item that is the parameter for the table.js shortcode, used like so in a markdown file: ---
phrase: cause pails trite
---
{{ table phrase }} And I want to use filters in that shortcode. So I need to pass both |
Beta Was this translation helpful? Give feedback.
It'd look something like this:
Where my ./shortcodes/table.js looks something like this: