Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #12 from deni35/master
Browse files Browse the repository at this point in the history
External template render function
  • Loading branch information
TakenPilot committed Feb 29, 2016
2 parents 33358ce + 40b5990 commit 141dc7e
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions index.js
Expand Up @@ -65,13 +65,14 @@ function getPromises(obj, fn) {
}

module.exports = function (data, options) {
var dataDependencies;
options = options || {};
var dependencies = [];
Handlebars = instance();
var render = options.render || defaultRender;
//Go through a partials object

if (data) {
var dataDependencies;
if (isPromise(data)) {
dataDependencies = data.then(function (result) {
data = result;
Expand All @@ -93,6 +94,7 @@ module.exports = function (data, options) {
});
dependencies = dependencies.concat(partialDependencies);
}

//Go through a helpers object
if (options.helpers) {
var helperDependencies = getPromises(options.helpers, function (id, contents) {
Expand All @@ -113,12 +115,12 @@ module.exports = function (data, options) {
dependencies = dependencies.concat(helperDependencies);
}


return through.obj(function (file, enc, callback) {
var self = this;
Promise.all(dependencies).then(function () {
file.contents = new Buffer(Handlebars.compile(file.contents.toString())(data));
self.push(file);
var tpl = Handlebars.compile(file.contents.toString());
render.call( this, tpl, data, file );

}.bind(this)).catch(function (err) {
self.emit('error', new gutil.PluginError('gulp-static-handlebars', err));
}).finally(function () {
Expand All @@ -138,4 +140,9 @@ function instance(handlebarsInstance) {
return Handlebars;
}

function defaultRender(tpl, data, file) {
file.contents = new Buffer(tpl(data));
this.push(file);
}

module.exports.instance = instance;

0 comments on commit 141dc7e

Please sign in to comment.