问题描述
我使用gulp-handlebars将我的模板预编译为JS,但我无法使自定义的handlebars helpers也进行预编译。有谁知道是否有任何支持/预编译自定义帮助器方法的方法?
I am precompiling my templates into JS using gulp-handlebars but I am having trouble getting custom handlebars helpers to also precompile. Does anyone know if there is any support/way to precompile custom helper methods?
推荐答案
我注意到gulp-handlebars可以使用特定的句柄库,基本上覆盖了它的默认值。所以,通过创建我自己的模块并注册一些帮助器,并将它提供给句柄调用,事情就在本地工作。
I noticed that gulp-handlebars can use a specific handlebars library, essentially overriding its default. So, by just creating my own module and registering some helpers, and feeding that into the handlebars call, things are working locally.
// helpers.js
var handlebars = require('handlebars');
handlebars.registerHelper('upper', function(str){
return str.toUpperCase();
});
module.exports = handlebars;
然后在gulpfile中(如下所示):
And then in the gulpfile (something like this):
var handlebars = require('./src/js/helpers.js');
gulp.task('handlebars', function(){
gulp.src('src/html/*.html')
.pipe(gulp_handlebars({handlebars: handlebars})) // override library here
});
这篇关于如何预编译gulp-handlebars助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!