Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.
Closed
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
18 changes: 18 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,24 @@ fileinclude({
}
```

### `for` statement

```js
fileinclude({
context: {
arr: ['test1','test2']
}
});
```

```
<ul>
@@for(var i=0;i<arr.length;i++){
<li>`+arr[i]+`</li>
}
</ul>
```

### License
MIT

Expand Down
15 changes: 15 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ module.exports = function(opts) {
name: 'if',
handler: conditionalHandler
});
text = replaceOperator(text, {
prefix: opts.prefix,
suffix: opts.suffix,
name: 'for',
handler: forHandler
});
text = replaceVariable(text, data, opts);
text = replaceFunction(text, {
prefix: opts.prefix,
Expand All @@ -92,6 +98,15 @@ module.exports = function(opts) {
return condition ? inst.body : '';
}

function forHandler(inst){
var condition='var context = this; with (context) { var result=""; for' + inst.args + ' { result+=`' + inst.body + '`; } return result; }';
// jshint ignore: start
var result = new Function(condition).call(data);
// jshint ignore: end

return result;
}

function includeHandler(inst) {
var args = /[^)"\']*["\']([^"\']*)["\'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args);

Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/arr-result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<body>
<h1>view</h1>

<label>haoxin</label>

<label>12345</label>

</body>
</html>
3 changes: 3 additions & 0 deletions test/fixtures/arr.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@@for(var i=0;i<name.length;i++){
<label>`+name[i]+`</label>
}
9 changes: 9 additions & 0 deletions test/fixtures/index-05.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<body>
@@include('view.html')
@@include('./arr.html', {
"name": ["haoxin",12345]
})
</body>
</html>
41 changes: 41 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('## gulp-file-include', function() {
var result = fs.readFileSync('test/fixtures/result.html', 'utf8');
var resultJS = fs.readFileSync('test/fixtures/result.js', 'utf8');
var resultSamePrefix = fs.readFileSync('test/fixtures/sameprefix-result.html', 'utf8');
var resultArr = fs.readFileSync('test/fixtures/arr-result.html', 'utf8');

describe('# default', function() {
it('file', function(done) {
Expand Down Expand Up @@ -441,4 +442,44 @@ describe('## gulp-file-include', function() {
stream.end();
});
});

describe('# for statement', function() {
it('file', function(done) {
var file = new gutil.File({
path: 'test/fixtures/index-05.html',
contents: fs.readFileSync('test/fixtures/index-05.html')
});

var stream = fileIncludePlugin();
stream.on('data', function(newFile) {
should.exist(newFile);
should.exist(newFile.contents);

String(newFile.contents).should.equal(resultArr);
done();
});

stream.write(file);
stream.end();
});

it('stream', function(done) {
var file = new gutil.File({
path: 'test/fixtures/index-05.html',
contents: fs.createReadStream('test/fixtures/index-05.html')
});

var stream = fileIncludePlugin();
stream.on('data', function(newFile) {
should.exist(newFile);
should.exist(newFile.contents);

String(newFile.contents).should.equal(resultArr);
done();
});

stream.write(file);
stream.end();
});
});
});