我有一个包含数组的pug模板,我想将其传递给一个mixin,用于填充一些schema.org(json)标记。
以下是代码: profile.pug
然后在mixin中,我有:
mixin.pug
一切都很好,除了“sameAs”链接的数组。编译后,我得到:
我需要的不是这个,而是...
以下是代码: 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"]
!{person.links}
或者person.links
呢? - Costa Michailidis!{...}
is unescaped string interpolation, and it just prints the values of whatever it contains as a string. Array markup (brackets, quotes) isn't part of the value of the array.Whenever I have to use js like this, I like to write it as a function using a code block in a separate.pug
file. That way you can just include your functions file and keep your pug cleaner by writing something likeprintArray(person.links)
- Sean