Created
November 11, 2014 06:55
-
-
Save lanceharper/5250c596c401c0539e75 to your computer and use it in GitHub Desktop.
layout with consolidate and gulp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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