Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass array from pug to pug mixin?

I have a pug template with an array in it, that I want to pass over to a mixin used to populate some schema.org (json) markup.

Here's the code:

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)

And then in the mixin, I have:

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}
    }

Everything works great, except the array of 'sameAs' links. After compiling, I get:

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

Instead of what I need, which is

"sameAs": ["https://link.one","https://link.two"]
like image 884
Costa Michailidis Avatar asked Oct 15 '25 16:10

Costa Michailidis


1 Answers

You could use JSON.stringify in conjuction with Unescaped interpolation !{} to get your array as a string:

"sameAs": !{JSON.stringify(person.links)}
like image 108
YouneL Avatar answered Oct 17 '25 06:10

YouneL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!