gulp-tpl
[data(yaml/json)] + [data(gulp control)] + [filter(js)] + tpl(handlebars/ejs) -> html
1 tpl -> [1..n] html
State
![NPM](https://nodei.co/npm/gulp-tpl.png?downloads=true&stars=true)
Usage
Note
-
data file: demo.yaml / demo.json
-
filter file: demo.filter.js
-
tpl file: demo.hbs / demo.ejs
-
note1: base name is same demo
-
note2: must *.filter.js
data + tpl -> html
var gulp = require('gulp');
var savefile = require('gulp-savefile');
var tpl = require('gulp-tpl');
gulp.task('default', function() {
return gulp.src('demo.hbs')
.pipe(tpl.html())
.pipe(savefile());
});
name: world
<div>{{name}}</div>
<div>world</div>
data + filter + tpl -> html
var gulp = require('gulp');
var savefile = require('gulp-savefile');
var tpl = require('gulp-tpl');
gulp.task('default', function() {
return gulp.src('demo.hbs')
.pipe(tpl.html())
.pipe(savefile());
});
name: world
module.exports.filter = function(data) {
data.name = "hello " + data.name;
return data;
};
<div>{{name}}</div>
<div>hello world</div>
data + gulp + filter + tpl -> html
var gulp = require('gulp');
var savefile = require('gulp-savefile');
var tpl = require('gulp-tpl');
gulp.task('default', function() {
return gulp.src('demo.hbs')
.pipe(tpl.html({data:{v2:"from gulp"}}))
.pipe(savefile());
});
v1: from yaml
module.exports.filter = function(data) {
data.v3 = "from filter";
return data;
};
<div>v1 {{v1}}</div>
<div>v2 {{v2}}</div>
<div>v3 {{v3}}</div>
<div>v1 from yaml</div>
<div>v2 from gulp</div>
<div>v3 from filter</div>
1 tpl -> [1..n] html
var gulp = require('gulp');
var savefile = require('gulp-savefile');
var tpl = require('gulp-tpl');
gulp.task('default', function() {
return gulp.src('demo.hbs')
.pipe(tpl.html())
.pipe(savefile());
});
module.exports.filter = function(data) {
datas = [];
data.ispc = true;
datas.push({filename:"demo_pc", data:data});
data = JSON.parse(JSON.stringify(data))
data.ispc = false;
datas.push({filename:"demo_mobile", data:data});
return datas;
};
{{#if ispc}}
<div>pc</div>
{{else}}
<div>mobile</div>
{{/if}}
<div>pc</div>
<div>mobile</div>
option
ignore error, only log error msg, easy to watch and debug
var gulp = require('gulp');
var savefile = require('gulp-savefile');
var tpl = require('gulp-tpl');
gulp.task('default', function() {
return gulp.src('demo.hbs')
.pipe(tpl.html({ignoreErr:true}))
.pipe(savefile());
});
Test
License
MIT License