Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useFileContents option #41

Merged
merged 1 commit into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -8,7 +8,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);
});

});