27.5 C
Lào Cai
Wednesday, July 8, 2020

How to make a handlebars helper global (in expressjs)

- Advertisement -
- Advertisement -

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

This helped me bridge the gap of passing the handlebars object with the layout back to the engine. 

- Advertisement -
Dung Vuhttps://www.iwmce.com
Share technology knowledge together. Learn experience to develop yourself.

Latest news

How to make a handlebars helper global (in expressjs)

in helpers/handlebars.js: var register = function(Handlebars) { var helpers = { inc: function(value,...
- Advertisement -

How to Fix ERROR 1115 (42000): Unknown character set: ‘utf8mb4’

Your version does not support that character set, I believe it was 5.5.3 that introduced it. You should upgrade your mysql to the version...

Close all tabs, but not the window, in Sublime Text

My way is revert all files after to close all files: View/Show Console and enter command:

Modern Monochrome Home with Calm and Cosy Terrace and Steps

All right. Well, take care yourself. I guess that's what you're best, presence old master? A tremor in the Force. The last...

Related news

How to make a handlebars helper global (in expressjs)

in helpers/handlebars.js: var register = function(Handlebars) { var helpers = { inc: function(value,...

How to Fix ERROR 1115 (42000): Unknown character set: ‘utf8mb4’

Your version does not support that character set, I believe it was 5.5.3 that introduced it. You should upgrade your mysql to the version...

Close all tabs, but not the window, in Sublime Text

My way is revert all files after to close all files: View/Show Console and enter command:

Modern Monochrome Home with Calm and Cosy Terrace and Steps

All right. Well, take care yourself. I guess that's what you're best, presence old master? A tremor in the Force. The last...
- Advertisement -

BÌNH LUẬN

Vui lòng nhập bình luận của bạn
Vui lòng nhập tên của bạn ở đây