22.7 C
Lào Cai
Monday, September 27, 2021

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

NSW records 18 new COVID-19 cases, lockdown decision likely on Wednesday

NSW Premier Gladys Berejiklian says she hopes to announce on Wednesday whether lockdown will be extended or not. Ms...
- Advertisement -

Scroll to a div using jQuery

Trước tiên bạn phải khai báo một id ở vị trí muốn Scroll tới. Ở đây tôi khai báo là

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...

Related news

NSW records 18 new COVID-19 cases, lockdown decision likely on Wednesday

NSW Premier Gladys Berejiklian says she hopes to announce on Wednesday whether lockdown will be extended or not. Ms...

Scroll to a div using jQuery

Trước tiên bạn phải khai báo một id ở vị trí muốn Scroll tới. Ở đây tôi khai báo là

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...
- 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