2016-12-30 80 views
1

在我的assemblefile.js中,我尝试注册一个自定义帮助程序。这个帮手本身确实有用,因为我已经在使用汇编的咕噜声项目中使用它了。如何在汇编中注册自定义handelbars助手0.17.1

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

在汇编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;; 
    }); 

}; 

回答

1

assemble现在是建立在templates模块的顶部,因此您可以使用.helper.helpers方式进行注册与组装帮手,将它们注册与把手。 This link有关于注册助手的更多信息。

由于使用了templates api,因此您不必在示例中使用.register方法包装帮助器。你可以只输出辅助功能,然后用注册时将其命名为组装这样的:

// 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方法注册对象时,属性键用于帮助程序名称

+0

非常感谢。有用!!! – Majabee