Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars registerHelper serverside with Expressjs

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.

like image 445
knowledgeseeker Avatar asked Jan 02 '17 08:01

knowledgeseeker


2 Answers

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

like image 200
Tom22 Avatar answered Oct 07 '22 15:10

Tom22


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/

like image 34
Mukesh Sharma Avatar answered Oct 07 '22 17:10

Mukesh Sharma