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 #8 from OrKoN/master
Browse files Browse the repository at this point in the history
Possibility to set/get Handlebars instance
  • Loading branch information
TakenPilot committed Mar 28, 2015
2 parents b137f46 + d9ea6bd commit 288f629
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var gutil = require('gulp-util');
var through = require('through2');
var Handlebars = require('handlebars');
var Handlebars = null;
var Promise = require('bluebird');
var _ = require('lodash');
var Path = require('path');
Expand Down Expand Up @@ -59,7 +59,7 @@ function getPromises(obj, fn) {
} else if (isPromise(obj)) {
obj.then(function (result) {
getPromises(result, fn);
})
});
}
return [];
}
Expand All @@ -68,6 +68,7 @@ module.exports = function (data, options) {
var dataDependencies;
options = options || {};
var dependencies = [];
Handlebars = instance();
//Go through a partials object

if (data) {
Expand Down Expand Up @@ -126,4 +127,15 @@ module.exports = function (data, options) {
});
};

module.exports.Handlebars = Handlebars;
function instance(handlebarsInstance) {
if (arguments.length == 1) {
Handlebars = handlebarsInstance;
} else {
if (!Handlebars) {
Handlebars = require('handlebars');
}
}
return Handlebars;
}

module.exports.instance = instance;
13 changes: 11 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function getPartials() {
gulp.src('./app/index.hbs')
.pipe(handlebars(getData(), {helpers: getHelpers(), partials: getPartials()}))
.pipe(gulp.dest('./dist'));

```

## Another example with vinyl pipes
Expand All @@ -48,6 +48,15 @@ gulp.src('./app/index.hbs')

```

## Get/Set Handlebars Instance
```JavaScript

var MyHandlebars = handlebars.instance() // get Handlebars
handlebars.instance(MyHandlebars) // use another Handlebars instance

```


## Install

```Sh
Expand All @@ -58,7 +67,7 @@ npm install gulp-static-handlebars

## Running Tests

To run the basic tests, just run `mocha` normally.
To run the basic tests, just run `mocha` normally.

This assumes you've already installed the local npm packages with `npm install`.

Expand Down
21 changes: 16 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function deferES6Promise() {
* Clear away all the helpers and partials that we may create in our tests.
*/
function clearHandlebars() {
handlebars.Handlebars.unregisterPartial('test');
handlebars.Handlebars.unregisterHelper('helper-function-export');
handlebars.Handlebars.unregisterHelper('test');
handlebars.instance().unregisterPartial('test');
handlebars.instance().unregisterHelper('helper-function-export');
handlebars.instance().unregisterHelper('test');
}

/**
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('Gulp Static Handlebars', function () {
.pipe(handlebars({contents: "contents!!"}, {partials: {'test': deferred.promise}}))
.pipe(gUtil.buffer(function (err, files) {
expect(files).to.have.length(lengthTest);
done()
done();
}));
deferred.resolve(partial);

Expand Down Expand Up @@ -485,4 +485,15 @@ describe('Gulp Static Handlebars', function () {
done();
});
});
});

it('can use another Handlebars instance', function (done) {
var Handlebars = {
'test' : 'Handlebars'
};
handlebars.instance(Handlebars);
expect(Handlebars).to.be.equal(handlebars.instance());
handlebars.instance(null); // use default
done();
});

});

0 comments on commit 288f629

Please sign in to comment.