Skip to content
Open
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ gulp.task('default', ['compile']);

**trace**: [true|false] *enables tracing info logging (defaults to false)*

**includes**: [string|array] *used to pass inline includes to Twig. (defaults to undefined) [Read more here](https://github.com/justjohn/twig.js/wiki#inline-templates) *

**getIncludesId**: [function] *can be used to implement a custom getter for the id (essentially the path) for inline templates passed in `includes`. (defaults to returning `filePath`) [Read more here](https://github.com/justjohn/twig.js/wiki#inline-templates)*
```javascript
// default
function(filePath) {
return filePath;
}

// example implementation
function(filePath) {
return path.relative('./my/source/path', filePath);
}
```

**extend**: [function (Twig)] *extends Twig with new tags types. The Twig attribute is Twig.js's internal object. [Read more here](https://github.com/justjohn/twig.js/wiki/Extending-twig.js-With-Custom-Tags)*

**functions**: [array] *extends Twig with given function objects. (default to undefined)*
Expand Down
60 changes: 52 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
var Twig = require('twig');
var map = require('map-stream');
var rext = require('replace-ext');
var gutil = require('gulp-util');
var glob = require('glob');
var fs = require('fs');
var _ = {
merge: require('lodash.merge'),
isArray: require('lodash.isarray')
};

const PLUGIN_NAME = 'gulp-twig';

var twig = Twig.twig,
includeCache = {};

module.exports = function (options) {
'use strict';
if (!options) {
options = {};

options = _.merge({
data: {},
includes: null,
getIncludeId: function(filePath) {
return filePath;
}
}, options || {});

// Register includes
// Thanks to https://github.com/backflip/gulp-twig
if (options.includes) {
if (!_.isArray(options.includes)) {
options.includes = [options.includes];
}

options.includes.forEach(function(pattern) {
glob.sync(pattern).forEach(function(file) {
var id = options.getIncludeId(file),
changed = fs.statSync(file).mtime,
content;

// Skip registering if include has not changed since last time
if (includeCache[id] && includeCache[id].getTime() === changed.getTime()) {
return;
}

includeCache[id] = changed;

content = fs.readFileSync(file, 'utf8').toString();

twig({
id: id,
data: content
});
});
});
}

function modifyContents(file, cb) {

var data = file.data || options.data || {};
var data = file.data || (typeof options.data === 'function' ? options.data(file) : options.data);

if (file.isNull()) {
return cb(null, file);
Expand All @@ -21,17 +66,16 @@ module.exports = function (options) {
if (file.isStream()) {
return cb(new gutil.PluginError(PLUGIN_NAME, "Streaming not supported!"));
}

data._file = file;
data._target = {
path: rext(file.path, '.html'),
relative: rext(file.relative, '.html')
};

var Twig = require('twig'),
twig = Twig.twig,
twigOpts = {
path: file.path,
var twigOpts = {
allowInlineIncludes: true,
data: file.contents.toString(),
async: false
},
template;
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
"author": "Simon de Turck <simon@zimmen.com> (http://www.zimmen.com)",
"main": "./index.js",
"dependencies": {
"glob": "5.0.6",
"gulp-util": "^2.2.14",
"lodash.isarray": "^3.0.4",
"lodash.merge": "^3.3.2",
"map-stream": "^0.1.0",
"replace-ext": "0.0.1",
"twig": "0.8.2"
"twig": "git://github.com/d-simon/twig.js.git#528dd7ce36e4bdb616c58b786b1a59e646fa24f2"
},
"devDependencies": {
"mocha": "*",
Expand Down