Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a helper with express-handlebars

I'm trying to load this helper on my Express / Handlebars project, but, I can't manage to make it work...

Here is my app.js

var express = require('express'),
    exphbs  = require('express-handlebars');

var app = express();

app.engine('handlebars', exphbs({
  defaultLayout: 'main',
  helpers: require('handlebars-form-helpers').helpers
}));
app.set('view engine', 'handlebars');

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

app.listen(3000);

And here is the page when I try to load it

enter image description here

Kind of new with Handlebars integration with Express, so, I can't manage to figure it out...

like image 483
Axiol Avatar asked Feb 11 '26 07:02

Axiol


1 Answers

This should work:

var exphbs = require('express-handlebars'),
    handlebars = require('handlebars'),
    helpers = require('handlebars-form-helpers').register(handlebars);

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

app.engine('.hbs', hbs.engine);
app.set('view engine', '.hbs');

You can find more info here: https://github.com/ericf/express-handlebars/blob/master/examples/advanced/server.js

like image 106
gluciano Avatar answered Feb 13 '26 18:02

gluciano