拥有以下汇编文件,并且所有内容都按预期加载。如果我使用把手助手,该文件编译,但它缺少我需要的助手。所以我不得不使用自定义助手文件/模块
var gulp = require('gulp');
var rename = require('gulp-rename');
var assemble = require('assemble');
//var helpers = require('handlebars-helpers');
var app = assemble();
var prettify = require('gulp-prettify');
var config = require('../config');
gulp.task('init', function(cb) {
app.helpers(config.paths.path_src + 'partials/_helpers/is-helper.js');
//app.helper('compare', helpers.comparison());
app.partials(config.paths.path_src + 'templates/includes/**/*.hbs');
app.layouts(config.paths.path_src + 'templates/layouts/**/*.hbs');
app.pages(config.paths.path_src + 'templates/content/**/*.hbs');
cb();
});
gulp.task('assemble', ['init'], function() {
return app.toStream('pages')
.pipe(app.renderFile())
.pipe(prettify({
indent_inner_html: false,
preserve_newlines: true,
end_with_newline: true,
extra_liners: ['head', 'body']
}))
.pipe(rename({
extname: '.html'
}))
.pipe(rename({dirname: ''}))
.pipe(app.dest('site'));
});
gulp.task('test', ['assemble']);
现在我已经尝试了把手 - 助手但它没有我正在使用的助手文件所以我不得不使用下面的那个:
/*
Custom handlebars helpers which allows to make
{{#is a '===' b}}}
{{/is}}
*/
module.exports.register = function (handlebars) {
var isArray = function(value) {
return Object.prototype.toString.call(value) === '[object Array]';
}
var ExpressionRegistry = function() {
this.expressions = [];
};
ExpressionRegistry.prototype.add = function (operator, method) {
this.expressions[operator] = method;
};
ExpressionRegistry.prototype.call = function (operator, left, right) {
if ( ! this.expressions.hasOwnProperty(operator)) {
throw new Error('Unknown operator "'+operator+'"');
}
return this.expressions[operator](left, right);
};
var eR = new ExpressionRegistry;
eR.add('not', function(left, right) {
return left != right;
});
eR.add('>', function(left, right) {
return left > right;
});
eR.add('<', function(left, right) {
return left < right;
});
eR.add('>=', function(left, right) {
return left >= right;
});
eR.add('<=', function(left, right) {
return left <= right;
});
eR.add('==', function(left, right) {
return left == right;
});
eR.add('===', function(left, right) {
return left === right;
});
eR.add('!==', function(left, right) {
return left !== right;
});
eR.add('in', function(left, right) {
if ( ! isArray(right)) {
right = right.split(',');
}
return right.indexOf(left) !== -1;
});
var isHelper = function() {
var args = arguments
, left = args[0]
, operator = args[1]
, right = args[2]
, options = args[3]
;
// console.log(args);
if (args.length == 2) {
options = args[1];
if (left) return options.fn(this);
return options.inverse(this);
}
if (args.length == 3) {
right = args[1];
options = args[2];
if (left == right) return options.fn(this);
return options.inverse(this);
}
if (eR.call(operator, left, right)) {
return options.fn(this);
}
return options.inverse(this);
};
handlebars.registerHelper('is', isHelper);
return eR;
};
我一直得到以下错误,错过了帮助&#34;是&#34;。看起来它没有注册! 请帮助!!!
答案 0 :(得分:0)
is
帮助文档为here。
还有一种方法可以导入自定义助手吗?
这是here所描述的。
您可以使用require
:
// single helper (required module returns the helper function)
app.helper('is', require('./helpers/is'));
// multiple helpers (required module returns an object of helper functions... e.g. key/value pairs)
app.helpers(require('./helpers'));
上面的自定义帮助程序代码使用grunt-assemble
语法来注册帮助程序。在这种情况下,我会将您的助手更改为:
/*
Custom handlebars helpers which allows to make
{{#is a '===' b}}}
{{/is}}
*/
module.exports = function () {
var isArray = function(value) {
return Object.prototype.toString.call(value) === '[object Array]';
}
var ExpressionRegistry = function() {
this.expressions = [];
};
ExpressionRegistry.prototype.add = function (operator, method) {
this.expressions[operator] = method;
};
ExpressionRegistry.prototype.call = function (operator, left, right) {
if ( ! this.expressions.hasOwnProperty(operator)) {
throw new Error('Unknown operator "'+operator+'"');
}
return this.expressions[operator](left, right);
};
var eR = new ExpressionRegistry;
eR.add('not', function(left, right) {
return left != right;
});
eR.add('>', function(left, right) {
return left > right;
});
eR.add('<', function(left, right) {
return left < right;
});
eR.add('>=', function(left, right) {
return left >= right;
});
eR.add('<=', function(left, right) {
return left <= right;
});
eR.add('==', function(left, right) {
return left == right;
});
eR.add('===', function(left, right) {
return left === right;
});
eR.add('!==', function(left, right) {
return left !== right;
});
eR.add('in', function(left, right) {
if ( ! isArray(right)) {
right = right.split(',');
}
return right.indexOf(left) !== -1;
});
var isHelper = function() {
var args = arguments
, left = args[0]
, operator = args[1]
, right = args[2]
, options = args[3]
;
// console.log(args);
if (args.length == 2) {
options = args[1];
if (left) return options.fn(this);
return options.inverse(this);
}
if (args.length == 3) {
right = args[1];
options = args[2];
if (left == right) return options.fn(this);
return options.inverse(this);
}
if (eR.call(operator, left, right)) {
return options.fn(this);
}
return options.inverse(this);
};
return isHelper;
};
在汇编中,您可以通过执行以下操作加载帮助程序:
var isHelper = require(config.paths.path_src + 'partials/_helpers/is-helper'));
app.helper('is', isHelper());
请注意,is-helper
中返回的函数在传递给app.helper
之前会被调用。这允许您将自定义选项传递给帮助程序(例如,您在帮助程序中使用的自定义表达式),但这取决于您。
希望这有帮助。