Handlebars registerHelper serverside with Expressjs

13,002

Solution 1

I have given a thumbs up to @Mukesh Sharma as he very nearly had the syntax that worked for me and really led me to it.

He is doing a bit more to make it work for the front end I believe. All I needed was the following

// index.js

//in the declarations
const exphbs  = require('express-handlebars');

//when configuring the app view engine
app.engine('.hbs', exphbs({
  extname: '.hbs',
  helpers: require('./config/handlebars-helpers') //only need this
}));
app.set('view engine', '.hbs');

Then I have a simple file I include from where I have helper stuff .

// config/handlebars-helpers.js

module.exports = {
  ifeq: function(a, b, options){
    if (a === b) {
      return options.fn(this);
      }
    return options.inverse(this);
  },
  bar: function(){
    return "BAR!";
  }
}

No need to pass or import handlebars-express - including the simple object of helper functions as part of the exhbs options hash under helpers lets express hbs do all of the registering by it's own approach.

(Bonus ifeq is a tiny helper that compares two arguments and will show the block if true .. useful for something like:

class = "{{#ifeq thisUrl '/about'}}active{{/ifeq}}" .. to set a nav pill class as 'active' ) found it here https://gist.github.com/pheuter/3515945

Solution 2

I think express-generator just sets view engine to hbs only. To configure the hbs engine, you have to use express-handlebars.

e.g.

var app = express(),
exphbs = require("express-handlebars");

app.engine("hbs", exphbs({
  defaultLayout: "main",
  extname: ".hbs",
  helpers: require("./public/js/helpers.js").helpers, // same file that gets used on our client
  partialsDir: "views/partials/", // same as default, I just like to be explicit
  layoutsDir: "views/layouts/" // same as default, I just like to be explicit
}));
app.set("view engine", "hbs");

And, helpers.js

var register = function(Handlebars) {
  var helpers = {
    // put all of your helpers inside this object
    foo: function(){
        return "FOO";
    },
    bar: function(){
        return "BAR";
    }
  };

  if (Handlebars && typeof Handlebars.registerHelper === "function") {
    // register helpers
    for (var prop in helpers) {
        Handlebars.registerHelper(prop, helpers[prop]);
    }
  } else {
      // just return helpers object if we can't register helpers here
      return helpers;
  }

};

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

Source: http://www.codyrushing.com/using-handlebars-helpers-on-both-client-and-server/

Share:
13,002
knowledgeseeker
Author by

knowledgeseeker

Updated on July 16, 2022

Comments

  • knowledgeseeker
    knowledgeseeker almost 2 years

    I am using expressjs with handlebars as templating engine with following code in Webstorm IDE with express generator.There is no visible handlebars require in the code (I guess express generator has it someplace else which is not visible)

    var app = express();
    .
    .    
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'hbs');
    

    How do i use registerHelper on serverside in this situation ?

    My other renderings and partials are working.So handlebars is doing its work.Its just that registerHelper seems to be cause of worry.

  • knowledgeseeker
    knowledgeseeker over 7 years
    and then how to define helpers in helpers.js ?
  • Mukesh Sharma
    Mukesh Sharma over 7 years
    sample helpers.js is available on the blog itself.
  • Adam Bubela
    Adam Bubela over 5 years
    thumb up for simplicity
  • Bruno Lamps
    Bruno Lamps over 3 years
    thumbs really up, it's quick and organized!