1

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?

Georgy
  • 2,410
  • 3
  • 21
  • 35
  • 1
    You're right the [documentation](http://harpjs.com/docs/development/rules) is lacking. I'm not totally convinced this would work. According to [this](http://jade-lang.com/reference/iteration/) 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. – Brennan Apr 24 '15 at 20:38
  • Thank you @Brennan ! This approach has comprehensible logic. I edited the question, would you please read it? Thank you in advance. – Georgy Apr 24 '15 at 21:53
  • Hey @Georgy, I would be surprised if there wasn't nesting in jade. Will you post the error you're getting? I'm also not sure how the template will react when `s.test` is undefined in the `hello-brazil` object. – Brennan Apr 24 '15 at 22:04
  • @Brennan your guess that the problem is with _s.test is undefined in the hello-brazil object._ is right, everything works fine if it is defined. Would you please write the answer so I could mark it as a solution? I appreciate your time and care, thank you once again. – Georgy Apr 24 '15 at 22:35
  • happy to help! I'm glad we got it worked out. – Brennan Apr 25 '15 at 01:02

2 Answers2

1

Following example probably answers the question:

<h1><%= title %></h1>
<ul>
  <% for (var slug in public.posts._data) { %>
    <% var post = public.posts._data[slug] %> 
    <li>
      <a href="posts/<%= slug %>">
        <%= post.title %>
      </a>
    </li>
  <% } %>
</ul>
Jarle
  • 11
  • 1
0

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.

Brennan
  • 1,764
  • 1
  • 14
  • 17