如何将数组从 pug 传递到 pug mixin?

How to pass array from pug to pug mixin?

我有一个带有数组的哈巴狗模板,我想将其传递给用于填充一些 schema.org (json) 标记的 mixin。

代码如下:

profile.pug

include ../components/bio-seo

- var person = {}
- person.title = "Name, title of person"
- person.url = "https://page.url..."
- person.image = "https://images.url..."
- person.links = ["https://link.one...","https://link.two..."]

+bio-seo(person)

然后在 mixin 中,我有:

mixin.pug

mixin bio-seo(person)
  title= title
  link(rel='canonical', href=url)

  script(type="application/ld+json").
    {
      "@context": "http://schema.org",
      "@type": "Person",
      "image": "#{person.image}",
      "url": "#{person.url}",
      "sameAs": #{person.links}
    }

一切正常,除了 'sameAs' 链接数组。编译后得到:

"sameAs": https://link.one,https://link.two

而不是我需要的,也就是

"sameAs": ["https://link.one","https://link.two"]

您可以将 JSON.stringifyUnescaped interpolation !{} 结合使用,以将数组作为字符串:

"sameAs": !{JSON.stringify(person.links)}