简体   繁体   English

如何将带有 Twig 的模板渲染为 Nodejs 中的字符串?

[英]How to render a template with Twig to string in Nodejs?

I have an template in Twig( https://www.npmjs.com/package/twig ) and I want to get it render to string.我在 Twig 中有一个模板( https://www.npmjs.com/package/twig ),我想让它呈现为字符串。 I'm using TS.我正在使用 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.但是 promise 始终处于挂起状态,即使我使用回调版本我也无法拥有 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...通过此调用,不存在默认导出的 TS 警报...

Thanks!谢谢!

Looks like this package has incorrect types看起来这个 package 的类型不正确

Try to use @ts-ignore and pass arguments like this:尝试使用@ts-ignore 并像这样传递 arguments :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM