我正在尝试做一些从 Assemble 看起来相对简单的事情我看过文档和其他 repo 协议(protocol),但由于某种原因,我在注册 Handlebars 助手时遇到了问题。助手在 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:
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']
},
}
模板代码:
{{#classgrid @index}}
// do something here
{{/classgrid}}
现在,当我在我的 Handlerbars 模板中实现我的助手并运行包含 assemble 任务的 grunt 任务时,我得到了错误
Warning: Missing helper: 'classgrid' Use --force to continue.
我不确定我做错了什么,或者我是否必须为我的助手创建一个单独的 NPM 包,这似乎在 assemble 文档中建议。我看过这 2 个 repo 协议(protocol),它们似乎在做我想做的事情
https://github.com/buildingblocks/bb-prototype-website/blob/master/Gruntfile.js https://github.com/ghost-town/layouts-example/blob/master/Gruntfile.js#L33
最佳答案
不确定这是否只是一个复制/粘贴问题,但上面的代码看起来不正确...下面是应该起作用的:
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);
});
};
我将尝试创建一个测试项目以确保其正常工作。
编辑:创建测试项目后,我发现您使用的是 Handlebars.register
而不是 Handlebars.registerHelper
。我已将代码更新为有效的解决方案。希望这会有所帮助。
关于javascript - 注册 Assemble Handlebars 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24418604/