can't register handlebar helpers

浪尽此生 提交于 2019-12-25 01:28:07

问题


I'm having trouble registering a custom helper in handlebars. I've tried anything I could find but nothing works.

here is what I have in app.js

const express= require('express');
const exphbs  = require('express-handlebars');
var socket = require('socket.io')
const app=express();
const querystring = require('querystring');

var hbs = exphbs.create({
    extname: '.hbs',
    defaultLayout: 'default',
    layoutsDir: './lib/templates/layouts',
    partialsDir: './lib/templates/partials',
    helpers: require("./helpers/handlebars.js").helpers
});

app.engine('hbs', hbs.engine);

//Handlebars Middleware
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(express.static('public'));

and I have in helpers/handlebars.js this code:

var register = function (Handlebars) {
    var helpers = {
        formatCurrency: function (currency) {
            return currency.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
        },
        format_date: function (date, format) {
            return moment(date).format(format);
        }
    };

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

(which is an example I found online)

But when I go to my view and try to call this helper:

{{formatCurrency settings.Ngames}}

I get this error:

Error: Missing helper: "formatCurrency"

I've tried also use in Handlebars.register in app.js but it doesn't recognize Handlebars.

What am I doing wrong? How can I register helpers?

Thank you!


回答1:


You should register the Handlebar helpers.

Posting my full code:

const express = require('express');
const exphbs = require('express-handlebars');

const app = express();

var hbs = exphbs.create({
  extname: '.hbs',
  defaultLayout: 'default',
  layoutsDir: './lib/templates/layouts',
  partialsDir: './lib/templates/partials'
});

require("./helpers/handlebars").register(hbs.handlebars);

app.engine('hbs', hbs.engine);

//Handlebars Middleware
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
app.use(express.static('public'));

const router = express.Router();

router.get("/hello", function (req, res) {
  res.render("index", { layout: false });
});

app.use(router);
app.listen(8080);

The helpers file:

var register = function (Handlebars) {
    var helpers = {
        formatCurrency: function (currency) {
            return currency.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
        },
        format_date: function (date, format) {
            return moment(date).format(format);
        }
    };

    if (Handlebars && typeof Handlebars.registerHelper === "function") {
        for (var prop in helpers) {
            Handlebars.registerHelper(prop, helpers[prop]);
        }
    } else {
        return helpers;
    }

};

module.exports.register = register;


来源:https://stackoverflow.com/questions/50209282/cant-register-handlebar-helpers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!