在 Pug 中打印变量而不包装标签

Printing variable without wrapping tag in Pug

我想在标签内打印一些任意内容。内容应该从Javascript开始设置。这是一个最小的例子:

pug.compileFile("templates/question.pug")({
   headExtraContent: "<script>console.log('hello')</script>",
});
doctype
   head
      title Some page title
      // headExtraContent should go here in an unescaped form
   body
      ...

我已经尝试了 #{headExtraContent}={headExtraContent、none 的方法。

有没有办法在 Pug 中实现这一点?

根据 Pug.js docs!{headExtraContent} 将插入内容而不转义。

您可以结合哈巴狗文档中描述的 unescaped string interpolation syntax Declan mentions with the text block in a tag 技术。

在你的情况下,它看起来像这样:

doctype
  head
    title Some page title
    .
      !{headExtraContent}
  body
    ...

如有疑问,documentation 99% 的情况下都能满足您的需求。