1

在我的 assemblefile.js 中,我尝试注册一个自定义助手。助手本身确实有效,因为我在一个带有 assemble 的 grunt 项目中使用了它。

assemble: {
  options: {
    helpers: ['./src/helper/custom-helper.js' ]
  }
}

在 assemble 0.17.1 我试过这样但它不起作用。有谁知道如何做到这一点?

app.helpers('./src/helper/custom-helper.js');

自定义helper.js:

module.exports.register = function (Handlebars, options, params)  {

    Handlebars.registerHelper('section', function(name, options) {
        if (!this.sections) {
        this.sections = {};
    }
    this.sections[name] = options.fn(this);
    return null;;
    });

};
4

1 回答 1

1

assemble现在构建在templates模块之上,因此您可以使用.helper.helpers方法向 assemble 注册帮助程序,这将向 Handlebars 注册它们。此链接包含有关注册助手的更多信息。

由于templates使用了 api,因此您不必使用.register示例中的方法包装帮助程序。您可以只导出辅助函数,然后在向 assemble 注册时将其命名,如下所示:

// custom-helper.js
module.exports = function(name, options) {
  if (!this.sections) {
    this.sections = {};
  }
  this.sections[name] = options.fn(this);
  return null;
};

// register with assemble
var app = assemble();
app.helper('section', require('./custom-helper.js'));

.helpers您还可以使用帮助器导出对象并使用以下方法一次性注册它们:

// my-helpers.js
module.exports = {
  foo: function(str) { return 'FOO: ' + str; },
  bar: function(str) { return 'BAR: ' + str; },
  baz: function(str) { return 'BAZ: ' + str; }
};

// register with assemble
var app = assemble();
app.helpers(require('./my-helpers.js'));

使用.helpers方法注册对象时,属性键用于帮助器名称

于 2016-12-31T20:33:27.670 回答