无法加载 Handlebars 辅助函数

Cannot load Handlebars helper function

我正在尝试创建一个自定义 hbs 帮助程序,并在我的 express.js 应用程序的页面中使用它。这是我的操作方法,但它一直在说:

Missing Helper "if_eq"

我的页面:

<html>
<head>
    <script src="javascripts/handlebars-v4.0.6.js" type="text/javascript"></script>
    <script src="javascripts/hbs_funcs.js"></script>
</head>
<body>
    {{#if_eq page "home"}}
            <li class="active"><a  href="/home ">LOBBY</a></li>
    {{else}}
            <li><a  href="/home ">LOBBY</a></li>
   {{/if_eq}}
</body>
</html>

这是我的 hbs js 文件:

Handlebars.registerHelper('if_eq', function (a, b, opts) {
if (a == b) // Or === depending on your needs
    return opts.fn(this);
else
    return opts.inverse(this);

});

我通过将函数移动到服务器端解决了这个问题,如下所示:

var hbs = exphbs.create({
defaultLayout: 'main', //we will be creating this layout shortly
helpers: {
    if_eq: function (a, b, opts) {
        if (a == b) // Or === depending on your needs
            return opts.fn(this);
        else
            return opts.inverse(this);
    }
}
});

HBS 模板在后端呈现,但您的助手已在前端注册。在您使用的地方注册助手。