How to render a template with Twig to string in Nodejs?

Viewed 1204

I have an template in Twig(https://www.npmjs.com/package/twig) and I want to get it render to string. I'm using TS.

function getHtml(title: string, message: string): Promise<string> {
  const html = await promisify(renderFile)('./template.twig', {
    filename: 'template.twig',
    settings: {
       title,
       message,
    },
  });
  return html;
}

But the promise is always pending, even if I use the callback version I can't have the HTML.

import Twig from 'twig';
Twig.renderFile('./path/to/someFile.twig', {foo:'bar'}, (err, html) => {
  html; // compiled string
});

And with this call the TS alert that doesn't exist default export...

Thanks!

1 Answers

Looks like this package has incorrect types

Try to use @ts-ignore and pass arguments like this:

// @ts-ignore
promisify(renderFile)('./template.twig', {message: 'Hello'})
    .then(html => console.log(html)); // Message: Hello
Related