汇编0.6.0:如何将多个模板应用于源文件?

时间:2015-04-30 15:23:00

标签: assemble

我希望Assemble获取我的源文件(/src/hbs/*.hbs)并创建两个单独的文件(例如,一个带有页眉,页脚和导航,另一个只带有代码片段)。我创建了两个模板(默认(整页)和空(只是一个包装))。我使用gulp汇编它看起来像这样:

gulp.task('assemble', function () {
  assemble.layouts(paths.templates.layouts);
  assemble.partial(paths.templates.partials);

  gulp.src(paths.sources.handlebars)
    .pipe(gulpAssemble(assemble, { layout: 'default' }))
    .pipe(prettify())
    .pipe(rename({basename:'index', extname:'.html'}))
    .pipe(gulp.dest(paths.build.www));
});

gulp.task('snippet', function(){
  assemble.layouts(paths.templates.layouts);
  assemble.partial(paths.templates.partials);

  gulp.src(paths.sources.handlebars)
    .pipe(gulpAssemble(assemble, { layout: 'empty' }))
    .pipe(prettify())
    .pipe(rename({extname:'.html'}))
    .pipe(gulp.dest(paths.build.www));

});

当我运行gulp时,两个文件都包含在默认模板中。我错过了什么?

1 个答案:

答案 0 :(得分:1)

Brandon Merritt的例子中的代码基于Assemble的0.6.0 beta API。

这就是你现在如何在汇编中完成此任务(从v0.17.0开始):

var assemble = require('assemble');
var app = assemble();


// use a task for loading collections, so we can ensure views
// are loaded before `src` files, or re-loaded when watch is triggered
app.task('load', function(cb) {
  app.layouts(paths.templates.layouts);
  app.partial(paths.templates.partials);
  cb();
});

app.task('assemble', ['load'], function() {
  // with assemble or gulp, you need to return the stream
  // or call the callback to signal task completion
  return app.src(paths.sources.handlebars)
    .pipe(app.renderFile())
    .pipe(rename({extname:'.html'}))
    .pipe(app.dest(paths.build.www));
});

app.task('snippet', ['load'], function() {
  return app.src(paths.sources.handlebars)
    .pipe(app.renderFile({ layout: 'default' }))
    .pipe(rename({basename:'index', extname:'.html'}))
    .pipe(app.dest(paths.build.www));
});