ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 384 forks source link

Cannot loop Arrays coming froma Helper method #52

Closed boedlen closed 10 years ago

boedlen commented 10 years ago

Hi Eric,

First off, Great job on the module. I really enjoy it, but i have a problem that i think might be a bug :-)

I have a template where i loop over some categories like this:

{{#Categories}}
<div class="checkbox">
  <label>
    <input type="checkbox" name="category" value="{{key}}">{{name}}
  </label>
</div>
{{/Categories}}

This works great if i render my view like this:

app.get('/', function (req, res) {
    res.render('Home', { Categories: GlobalSettings.Categories });
});

But if i define a helper method that return the same array of Categories it doesn't work is i expect:

var hbs = exphbs.create({
    defaultLayout: 'main',
    helpers: {
        Categories: function () { return GlobalSettings.Categories }
    }
});

app.get('/', function (req, res) {
    res.render('Home');
});

When i add the array as a helper method my output is rendered like this:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Can i only return flat types from a helper method?

boedlen commented 10 years ago

I'm able to do what i want with app.locals, so there probably isn't an issue anyway :-)

var app = express();
app.locals({
    Categories: GlobalSettings.Categories
})
ericf commented 10 years ago

This a limitation in Handlebars. You can use subexpressions to call helpers in a tested fashion.