【发布时间】:2015-09-28 16:53:46
【问题描述】:
我正在尝试使用 express-hbs 在 gulp 任务中预编译一些静态 HTML(带有布局和部分),作为“发布此库”任务的一部分。我有一些看起来像
gulp.task 'compile', ->
gulp.src './www/views/*.html'
.pipe through.obj (file, enc, cb) ->
//the missing part
.pipe gulp.dest './temp'
我知道我可以调用 hbs compile 来编译单个文件,但这不包括任何部分或布局。
template = hbs.compile(file.contents.toString());
file.contents = new Buffer(template(data));
this.push(file);
cb();
我知道我只是在这里遗漏了一些简单的东西,查看 express-hbs 的来源我不确定我需要传递给该渲染函数以使其做正确的事情。
我想使用 express.hbs 插件,因为我们正在使用它添加到车把的一些帮助程序,例如 contentFor 等。
更新,解决方案:
gulp.task 'compile', ->
gulp.src './www/views/*.html'
.pipe through.obj (file, enc, cb) ->
render = hbs.create().express3
viewsDir: __base + 'views'
partialsDir: __base + 'views/partials'
layoutDir: __base + 'views/layouts'
defaultLayout: __base + 'views/layouts/layout.html'
extName: 'html'
locals = {
settings: {
views: __base + 'views'
}
}
self = this;
render file.path, locals, (err, html) ->
if(!err)
file.contents = new Buffer(html);
self.push(file);
cb();
else
console.log "failed to render #{file.path}"
.pipe gulp.dest './temp'
【问题讨论】:
标签: node.js express gulp handlebars.js