Skip to content

using filter in a shortcode defined in its own file #3192

Answered by pdehaan
btrem asked this question in Q&A
Discussion options

You must be logged in to vote

It'd look something like this:

const getFoo = require('./filters/get-foo.js');

module.exports = (eleventyConfig) => {
  eleventyConfig.addFilter("getFoo", getFoo);

  const table = require('./shortcodes/table.js')(eleventyConfig);
  eleventyConfig.addShortcode("table", table);

  return {
    dir: {
      input: "src",
      output: "www",
    },
  };
};

Where my ./shortcodes/table.js looks something like this:

module.exports = function (eleventyConfig = {}) {
  const foo = eleventyConfig.getFilter("getFoo");
  return (vals = []) => {
    const rows = vals.map(row => `<tr><td>${foo(row)}</td></tr>`);
    return `<table><tbody>${rows.join("\n")}</tbody></table>`;
  };
};

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@pdehaan
Comment options

Answer selected by btrem
@pdehaan
Comment options

Comment options

You must be logged in to vote
4 replies
@pdehaan
Comment options

@btrem
Comment options

@pdehaan
Comment options

@btrem
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants