无法加载Handlebars帮助器function

我正在尝试创build一个自定义的hbs帮助器,并在我的express.js应用程序的页面中使用它。 这是我如何做,但它一直说:

缺less帮手“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模板呈现在后端,但你的帮手在前端注册。 在你使用它的地方注册助手。