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

标签 html arrays pug interpolation

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

代码如下:

配置文件.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)}

关于html - 如何将数组从 pug 传递到 pug mixin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48408963/

相关文章:

html - 图片在 Safari 浏览器中不显示

python - 处理 PDF 以进行信息提取

javascript - 查找数组中的数字(两位数)

c - C 中结构数组的 'sizeof' 结果?

css - 使用 {% for post in site.posts %} 从特定子文件夹中提取

javascript - 使用 JavaScript 将富格式文本文件嵌入到 HTML 中

javascript - Animate.css 和音频

java - 如何在 Java 中将 List<Integer> 转换为 Integer[]?

node.js - 在 jade 中导入 CSV/JSON

javascript - 如何使用 res.render 在每次路由到页面时发送数据?