错误:Handlebars.js中缺少帮手 [英] Error: Missing Helper in Handlebars.js

查看:313
本文介绍了错误:Handlebars.js中缺少帮手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有节点和表达式的handlebars.js模板。我正在使用 {{@ index}} 模板标记编制一个编号列表,但是由于索引从0开始,我想从一开始,似乎我需要使用一个定制的帮手。我已经看到很多关于这个的帖子,我发现了以下代码:

  Handlebars.registerHelper(inc,函数(值,选项)
{
return parseInt(value)+ 1;
});

{{#each score}}
< li class =list-group-item>
< div id =place> {{inc @index}}& nbsp< / div>
< div class =wordOrName> {{player_name}}< / div>
< div class =number> {{score}}< / div>< / li>
{{/ each}}

我似乎无法找到帮助器寄存器功能应该去。我试着把它放在模板本身和其他各种地方,但我仍然不断收到

 错误:缺少帮手: inc
型号。< anonymous>

理想情况下,我希望帮助程序位于单独的helpers.js文件中,但我不对于如何让把手识别它有丝毫的想法。



编辑:

Handlebars包含在在节点文件index.js中使用下面的代码项目:

  //查看引擎
app.set(' views',__dirname +'/ views /');
app.set('view engine','handlebars');
app.engine('handlebars',engines.handlebars);

在模板本身中包含helper函数似乎是不可能的。

解决方案

我明白了......佣工确实需要在节点应用文件中注册,如下所示:

  //查看引擎
app.set('views',__dirname +'/ views /');
app.set('view engine','handlebars');
var hbs = require('handlebars');
hbs.registerHelper(inc,function(value,options)
{
return parseInt(value)+ 1;
});
app.engine('handlebars',engines.handlebars);

我希望这个信息更容易访问,但它就是这样。


I am using handlebars.js templates with node and express. I am making a numbered list using the {{@index}} template tag, however since index starts at 0 and I want to start from one, it seems I need to use a custom helper. I've seen plenty of posts regarding this and I've found the following code:

Handlebars.registerHelper("inc", function(value, options)
{
    return parseInt(value) + 1;
});

{{#each score}}
      <li class="list-group-item">
      <div id="place"> {{inc @index}} &nbsp </div>
      <div class="wordOrName">{{ player_name }}</div>
           <div class="number">{{ score }}</div></li>
        {{/each}}

What I cannot seem to find is where the helper register function is supposed to go. I've tried putting it inside in the template itself and in various other places but I still keep getting

Error: Missing helper: "inc"
   at model.<anonymous>

Ideally I'd like to have the helper in a separate file helpers.js but I don't have the slightest idea of how to get handlebars to recognize it.

EDIT:

Handlebars is included in the project with the following code inside the node file index.js:

// view engine
app.set('views', __dirname + '/views/');
app.set('view engine', 'handlebars');
app.engine('handlebars', engines.handlebars); 

It appears impossible to include the helper function within the template itself.

解决方案

I figured it out...The helpers indeed need to be registered in the node app file like so:

// view engine
app.set('views', __dirname + '/views/');
app.set('view engine', 'handlebars');
var hbs = require('handlebars');
hbs.registerHelper("inc", function(value, options)
{
    return parseInt(value) + 1;
});
app.engine('handlebars', engines.handlebars);

I wish this info was more easily accessible, but there it is.

这篇关于错误:Handlebars.js中缺少帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆