1

I am using the below code to create a new filter in twig

const twig = require("twig");

twig.extendFilter("date_format", (value) => {
  return value ? dateFNS.format(value.unixUTC, "MM/dd/yyyy") : null;
});

Similarly, how can we create a new filter in twing?

I have tried with below method

const { TwingFilter, TwingEnvironment, TwingLoaderFilesystem } = require("twing");
const format_date = new TwingFilter("format_date", (value) => {
    return value ? dateFNS.format(value.unixUTC, "MM/dd/yyyy") : null;
});
const loader = new TwingLoaderFilesystem([
    "./screen"
]);
const twing = new TwingEnvironment(loader);
twing.addFilter(format_date);

But it is giving below error

TypeError: callable.apply(...).catch is not a function

Eric MORAND
  • 6,373
  • 4
  • 27
  • 34
Benk I
  • 185
  • 13
  • Can you share more details? The given code does not look like Twig code after all – Nico Haase Dec 15 '21 at 11:25
  • 1
    Please have a lookt at the [documentation](https://nightlycommit.github.io/twing/advanced.html). It's clearly demonstrated there – DarkBee Dec 15 '21 at 11:29
  • I have created a new filter in express js. I want to do a similar thing in `Twing` Thanks. – Benk I Dec 15 '21 at 11:29
  • @DarkBee Please check the updated description. Before only I have implemented it by referring doc, but it is giving an error. So I have raised a question here. Please check that once. Thanks. – Benk I Dec 15 '21 at 11:54
  • I'm sorry, I'm not working with javascript/typescript at this moment, so I cannot answer this. Look like you need to pass a promise though, from what I can see – DarkBee Dec 15 '21 at 13:21
  • @DarkBee Thank it is worked. – Benk I Dec 15 '21 at 15:22
  • @DarkBee I just got one doubt. Can't we declare all our filters in app.js OR server.js and use it template? I have tried this and it is not working. i.e. When I declare a filter in the individual control file and used it in the template it is working whereas when I declare filter in app.js and try to use it in the template it shows an error. – Benk I Dec 15 '21 at 15:42
  • Check the [creating an extension](https://nightlycommit.github.io/twing/advanced.html#creating-an-extension) part of the documentation, that's how you would do it – DarkBee Dec 15 '21 at 17:49

0 Answers0