注册组装把手助手 [英] Register Assemble Handlebars Helpers

查看:193
本文介绍了注册组装把手助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从汇编文档和其他我已有的回购文件中完成一些看起来相对简单的事情看着,但由于某种原因,我注册我的Handlebars帮手时遇到了问题。该帮助程序位于helpers> helper-classgrid.js中。
$ b

I am trying to do something that seems relatively simple from the Assemble docs and other repos I've looked at but for some reason I'm having a problem registering my Handlebars helpers. The helper is in helpers > helper-classgrid.js

module.exports.register = function (Handlebars, options, params)  { 
  Handlebars.register('classgrid', function (index, options)  { 
    gridclass: function (index, options) {
    if (index === 0 || index % 4 === 0) {
        return options.fn(this);
      }
    return options.inverse(this);
  };
};

我的gruntfile其中config.helpers = helpers:
$ b $

My gruntfile where config.helpers = helpers:

assemble: {
      options: {
        layoutdir: '<%= config.guts %>/templates/layouts/',
        assetsDir: '<%= grunt.config.get("assets_dir") %>',
        environmentIsProduction: '<%= grunt.config.get("environmentIsProduction") %>',
        environmentIsDev: '<%= grunt.config.get("environmentIsDev") %>',
        data: ['<%= config.content %>/**/*.json', '<%= grunt.config.get("environmentData") %>'],
        helpers: ['<%= config.helpers %>/helper-*.js']
      },
}

模板代码:

Template code:

{{#classgrid @index}}
// do something here
{{/classgrid}}

现在,当我在Handlerbars模板中实现我的帮助程序并运行包含汇编任务的grunt任务时,出现错误消息

Now when I implement my helper in my Handlerbars template and run the grunt task containing the assemble task I get the error

Warning: Missing helper: 'classgrid' Use --force to continue.

我不确定我做了什么错误,或者是否需要创建单独的NPM包对于我的帮手,它似乎在汇编文档中提出。我已经看过了这两个似乎正在做我想做的事情的回购交易。

I'm not sure what I've done wrong or if I have to create a separate NPM package for my helpers which it seems to suggest in the assemble docs. I've looked at these 2 repos which seem to be doing what I'm trying to do

https://github.com/buildingblocks/bb-prototype-website/blob/master/Gruntfile.js
https://github.com/ghost-town/ layouts-example / blob / master / Gruntfile.js#L33

推荐答案

不知道这是否只是一个副本/ paste问题,但上面的代码看起来不正确...这是应该工作的:

Not sure if this was just a copy/paste issue, but the code above doesn't look correct... here's what should work:

module.exports.register = function (Handlebars, opts, params)  { 
  Handlebars.registerHelper('classgrid', function (index, options)  { 
    if (index === 0 || index % 4 === 0) {
      return options.fn(this);
    }
    return options.inverse(this);
  });
};

我会尝试创建一个测试项目以确保它正常工作。

I'll try to create a test project to make sure this is working.

编辑:在创建一个测试项目之后,我看到您正在使用 Handlebars.register 而不是 Handlebars。 registerHelper 。我已将代码更新为可行的解决方案。希望这有助于。

After creating a test project, I see that you were using Handlebars.register instead of Handlebars.registerHelper. I've updated the code to a working solution. Hope this helps.

这篇关于注册组装把手助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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