jaredly / hexo-renderer-handlebars

MIT License
26 stars 12 forks source link

Iterating over posts complicated #4

Open klaronix opened 9 years ago

klaronix commented 9 years ago

Just for curiosity: how do you iterate over page.posts/site.posts/tags, ...? After trying a lot of different ways, I ended up with two possibilities:

{{#with page.posts}}
{{#each ../page.posts.toArray}}
...
{{/each}
{/with}}

This works if you don't want to limit posts; you will iterate over all of them (can't change ordering either). Other possiblity: create a helper which converts query objects to an array:

{{#each (to_array page.posts) }}
...
{{/each}}

or

{{#each (to_array get_posts count=3 order=date) }}
...
{{/each}}

I would suggest adding this "to_array" as a standard helper. Any other possibilities?

jaredly commented 9 years ago

Yeah that sounds like a reasonable idea.

rapodaca commented 7 years ago

I found that this works:

{{#each page.posts.data as |post|}}
  <h2>{{post.title}}</h2>
{{/each}

but I expected to be able to do this instead, which is cleaner and reflects how the EJS documentation says this should work:

{{#each page.posts as |post|}}
  <h2>{{post.title}}</h2>
{{/each}

Why does the HBS Hexo plugin use this construct for the page.posts object?

{
  "data": [
    // array of posts
  ],
  "length": 1
}