Reputation: 1361
In my appjs file I am trying to display all article titles in descending order. This works fine, the problem is that mongoose returns an array and i'm not sure how to write this in handlebars.
var bmtitles = BlogModel.find().select('title date').sort("date", -1);
bmtitles.execFind(function(err, ttles) {
console.log(ttles);
var model = {
layout:'blog.hbs',
BmTitles: ttles,
};
//render page
res.render('blog', model);
});
I've tried things like these but none work:
{{BmTitles.n.title}}
{{BmTitles.title}}
{{BmTitles}}
This is what the ttles variable returns (only two posts for testing)
[ { _id: 5011b563a947b943dc32d6f5,
title: 'Blog title one',
date: Sun Jan 01 2012 18:00:00 GMT-0600 (CST) },
{ _id: 5011c155a947b943dc32d6f6,
title: 'secondpost',
date: Sat Dec 31 2011 18:00:00 GMT-0600 (CST) } ]
Upvotes: 1
Views: 3921
Reputation: 434975
You'd want to use #each
to iterate over your array:
The
each
block helperYou can iterate over a list using the built-in
each
helper. Inside the block, you can usethis
to reference the element being iterated over.
So something like this:
{{#each BmTitles}}
<p>{{_id}}: {{title}}</p>
{{/each}}
Demo: http://jsfiddle.net/ambiguous/vZyHn/
Or you could to it Mustache-style:
Sections
Sections render blocks of text one or more times, depending on the value of the key in the current context.
A section begins with a pound and ends with a slash. That is,
{{#person}}
begins a "person" section while{{/person}}
ends it.
So this will also work:
{{#BmTitles}}
<p>{{_id}}: {{title}}</p>
{{/BmTitles}}
Demo: http://jsfiddle.net/ambiguous/qyE9b/
Upvotes: 6