Skip to content

Commit

Permalink
Merge pull request #41 from manychat/master
Browse files Browse the repository at this point in the history
useFileContents option
  • Loading branch information
bezoerb authored Jan 8, 2018
2 parents 77e3ff0 + 5cfd6bb commit a895075
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ gulp.task('default', ['compile']);

**extname**: [string|true|false] *output file extension including the '.' like path.extname(filename). Use `true` to keep source extname and a "falsy" value to drop the file extension*

**useFileContents**: [true|false] *use file contents instead of file path (defaults to false) [Read more here](https://github.com/zimmen/gulp-twig/issues/30)*

**functions**: [array] *extends Twig with given function objects. (default to undefined)*
```javascript
[
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module.exports = function (options) {
'use strict';
options = Object.assign({}, {
changeExt: true,
extname: '.html'
extname: '.html',
useFileContents: false,

}, options || {});

function modifyContents(file, cb) {
Expand Down Expand Up @@ -77,6 +79,11 @@ module.exports = function (options) {
delete options.extend;
}

if (options.useFileContents) {
var fileContents = file.contents.toString();
twigOpts.data = fileContents
}

template = twig(twigOpts);

try {
Expand Down
22 changes: 22 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,26 @@ describe('gulp-twig', function () {
twg.write(fakeFile);
});

it('should use file contents if useFileContents option is enabled', function (done) {
var twg = twig({
useFileContents: true,
data: {
title: 'twig',
},
});

var fakeFile = new gutil.File({
base: 'test/',
cwd: 'test/',
path: path.join(__dirname, '/templates/file.twig'),
contents: new Buffer('{{ title }}'),
});

twg.on('data', function (newFile) {
String(newFile.contents).should.equal('twig');
done();
});
twg.write(fakeFile);
});

});

0 comments on commit a895075

Please sign in to comment.