TypeError: Handlebars.registerHelper is not a function

后端 未结 3 715
自闭症患者
自闭症患者 2021-02-02 03:59

I am brand new to node and handlebars as of two days ago so bear with me. I am trying to use custom handlebars helpers but am not entirely sure where to put it.

I keep g

3条回答
  •  佛祖请我去吃肉
    2021-02-02 04:44

    Just came across the same problem. Thanks to R.A.Lucas and Drkawashima, the answers above are correct, but here's a slightly shorter option.

    var express = require('express');
    var handlebars = require('express-handlebars');    
    var app = express();
    
    app.engine('handlebars', handlebars({
        helpers: {
            sayHello: function () { return "Hello"; },
            getStringifiedJson: function (value) {
                return JSON.stringify(value);
            }
        },
    partialsDir: ['views/partials/'],
    defaultLayout: 'main'
    }));
    app.set('view engine', 'handlebars');
    

提交回复
热议问题