How to make helper helper global (in expressjs)

I have a pretty simple handlebars helper file in helpers/handlebars.js:

var hbs = require('express-handlebars');

hbs.registerHelper("inc", function(value, options) {
    return parseInt(value) + 1;
});

However, as expected, I cannot reference the helper {{#inc}}because I did not pass it to the function res.render(). Is there a way to make all the helpers in my file global and "automatically included"?

change

Having tried @ 1cgonza's awesome answer, I get:

hbs.registerHelper("inc", function(value, options) {
      ^
TypeError: undefined is not a function

When you start the application. Here app.js:

var engine      = require('express-handlebars');
                  require('./helpers/handlebars.js')(engine);

app.engine('hbs',           engine({defaultLayout: 'layout', extname: 'hbs'}));
app.set('view engine',      'hbs');

Any ideas?

+4
source share
2 answers

You can try to export your helpers as a module, and then include them in your main app.js file

Something like that:

IN helpers/handlebars.js

function hbsHelpers(hbs) {
  hbs.registerHelper("inc", function(value, options) {
    return parseInt(value) + 1;
  });

  // More helpers...
}

module.exports = hbsHelpers;

app.js( , ).

var hbs = require('express-handlebars');
require('./helpers/handlebars')(hbs);

?

express-handlebars docs, helpers/handlebars.js - :

function hbsHelpers(hbs) {
  return hbs.create({
    helpers: { // This was missing
      inc: function(value, options) {
        console.log('reading it');
        return parseInt(value) + 1;
      }

      // More helpers...
    }

  });
}

module.exports = hbsHelpers;

, .

2:

My Bad, helpers:{} create() handelbars.js. , , , , .

app.js, , , , :

// I've changed this from engine to exphbs,
// so there is no confusion with the express engine object that we use later.
var exphbs = require('express-handlebars');

// Create an instance of the express-handlebars
// If you want to pass any option offered by express-handlebar module
// do it inside the create() in the handlebars.js file
var handlebars  = require('./helpers/handlebars.js')(exphbs);

// The handlebars variable now has an object called engine.
// Use that to define your app.engine
// As said before, you don't need to define any options here.
// Everything is defined in the create() in handlebars.js
app.engine('hbs', handlebars.engine);

// If you are using a different extension, you can change hbs to whatever you are using. 
app.set('view engine', 'hbs');
+10

:

/handlebars.js:

var register = function(Handlebars) {
    var helpers = {
    inc: function(value, options) {
        return parseInt(value) + 1;
    },
    foo: function(var1, var2) {
        return ....
    }
};

if (Handlebars && typeof Handlebars.registerHelper === "function") {
    for (var prop in helpers) {
        Handlebars.registerHelper(prop, helpers[prop]);
    }
} else {
    return helpers;
}

};

module.exports.register = register;
module.exports.helpers = register(null); 

app.js:

var exphbs = require('express-handlebars');
var hbsHelpers = exphbs.create({
    helpers: require("./helpers/handlebars.js").helpers,
    defaultLayout: 'layout',
    extname: '.hbs'
});

app.engine('.hbs', hbsHelpers.engine);
app.set('view engine', '.hbs');
+4

Source: https://habr.com/ru/post/1608401/


All Articles