如何将数组从哈巴狗传递给哈巴狗?

时间:2018-01-23 19:00:24

标签: html arrays pug interpolation

我有一个包含数组的哈巴狗模板,我想将其传递给用于填充一些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"]

1 个答案:

答案 0 :(得分:3)

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

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