Search code examples
jsonnode.jspugharp

In "for article, slug in public.articles._data" what is "slug"? Is there a way to use nesting arrays?


I experiment with HarpJS, a NodeJS static blog generator. In tutorials there is such example for metadata:

for article, slug in public.articles._data
 a(href="/articles/#{ slug }")
  h2= article.title

and _data.json file:

{
 "hello-world": {  <-- available everywhere as public.articles._data
  "title": "Hello World.",
  "date": "2013-02-28"
 },
 "hello-brazil": {
  "title": "Hello Brazil.",
  "date": "2013-03-04"
 }
}

If I understand right for article takes every high level object and we can get title with article.title or date with article.date. But what is slug? Is it the predefined variable in Jade/HarpJs? If so, are there others, or did I get the concept wrong? I couldn't find any information on this topic, if there is a good article to read I would appreciate it. Thank you.

As @Brennan suggests in comments the second argument could be an index. Simple substitution and renaming of variables article and slug proves that. But there is one more problem. Please consider this example:

{
 "hello-world": {
  "title": "Hello World.",
  "date": "2013-02-28",
  "test": {
    "testContent": "123"
  }
 },
 "hello-brazil": {
  "title": "Hello Brazil.",
  "date": "2013-03-04"
 }
}


for s, a in public.articles._data
 a(href="/articles/#{ a }")
  h2= s.title
  h3= s.date
  h3= a
  - var obj =  s.test
  h3= obj
  h3= obj.testContent

This code gives error during the compilation. If I comment the very last line it works though. And I can't substitute last line with snippet from the docs:

each val, index in obj
  h1=index + ': ' + val

There are no nesting and two-dimensional arrays?


Solution

  • To answer the original question see my comment:

    The documentation is lacking. I'm not totally convinced this would work. According to this jade only supports each and while constructs. In other languages, the 2nd argument in a for..in loop will be the index. It's possible that's the case here, but I can't find any concrete documentation

    The second question was an issue with the mock object.

    Looks like you need a check to be sure that your property is defined in your mockdata, or do some checking to be sure the property you expect, exists.