Skip to content

Instantly share code, notes, and snippets.

@lanceharper
Created November 11, 2014 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanceharper/5250c596c401c0539e75 to your computer and use it in GitHub Desktop.
Save lanceharper/5250c596c401c0539e75 to your computer and use it in GitHub Desktop.
layout with consolidate and gulp
var consolidate = require('consolidate');
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
const PLUGIN_NAME = 'gulp-layout';
function gulpLayout(opts) {
opts = opts || {};
if (!opts.engine) {
throw new PluginError(PLUGIN_NAME, '"engine" option required.');
}
var stream = through.obj(function(file, enc, cb) {
if (file.isNull()) {
// do nothing if no contents
}
function render(err, html) {
if (err) {
cb(err);
} else {
file.contents = new Buffer(html);
cb(null, file);
}
}
try {
consolidate[opts.engine]('templates/layout.hbs', { contents: String(file.contents)}, render);
} catch (err) {
cb(err);
}
});
return stream;
};
module.exports = gulpLayout;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment