Skip to content Skip to sidebar Skip to footer

How To Make A Handlebars Helper Global (in Expressjs)

I've got a pretty simple handlebars helper file in helpers/handlebars.js: var hbs = require('express-handlebars'); hbs.registerHelper('inc', function(value, options) { return

Solution 1:

You could try exporting your helpers as a module and then include them in your main app.js

Something like this:

In your helpers/handlebars.js

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

  // More helpers...
}

module.exports = hbsHelpers;

Then in your app.js (or the file you are using as the index).

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

Does that work for you?

EDIT

Based on the express-handlebars docs, I would change the function in your helpers/handlebars.js to something like this:

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;

Let us know if it works.

EDIT 2:

My Bad, wrapping your helpers inside a helpers:{} was missing from the create() function in the handelbars.js file. I've edited my previous answer, see where I placed the comment to know what I am talking about.

As for the app.js I think it is a little mixed up, so let me rename a few things to make it clear:

// 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');

Solution 2:

you can try:

in helpers/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); 

in 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');

Solution 3:

Here is my solution. You can register both your own customer helpers and the ones from 'handlebars-helpers':

const hbshelpers = require('handlebars-helpers');
const multihelpers = hbshelpers();

const helpersDM = {
    hlp: echo => `Echo: ${echo}.`,
    STATIC: `/static`,
};
const hbs = exphbs.create({
    layoutsDir: join(__dirname, 'views', 'layouts'),
    partialsDir: join(__dirname, 'views', 'partials'),
    extname: '.hbs',
    defaultLayout: 'base',
    helpers: {...multihelpers, ...helpersDM},
});
app.engine('.hbs', hbs.engine);
app.setViewEngine('.hbs');

Solution 4:

You can register your own helpers using following method,

In your app.js firstly import express-handlebars, express npm module,

const exphbs = require("express-handlebars");
const express = require("express");
const app = express();

Now configure the exphbs using create method which takes options as objects,

const myhbs = exphbs.create({/*config*/ extname:"hbs", helpers:{equal:function(a,b, options){return (a==b)?options.fn(this):options.inverse(this)}}});
app.engine("hbs", myhbs.engine);
app.set("view engine", "hbs");

By registering the partials like this one can use it throughout the handlebars as a inbuilt function.


Post a Comment for "How To Make A Handlebars Helper Global (in Expressjs)"