Last active
August 29, 2015 14:27
-
-
Save Billy-/cdfda83cc02ba4c0764e to your computer and use it in GitHub Desktop.
Gulp task for compiling handlebars templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
gulp.task('handlebars', ['font:icon'], function () { | |
var config = require('../config'); | |
var handlebars = require('gulp-compile-handlebars'); | |
var bs = require('browser-sync').get('gulp'); | |
var ext = require('gulp-ext'); | |
var plumber = require('gulp-plumber'); | |
var path = require('path'); | |
var fs = require('fs'); | |
var data = {}; | |
var dataFiles = fs.readdirSync(config.paths.handlebars.data); | |
dataFiles.forEach( function(file){ | |
data[file.slice(0, -5)] = require(path.resolve(config.paths.handlebars.data, file)); | |
}); | |
var helpers = { | |
partial: function(){ | |
var ps = handlebars.Handlebars.partials; | |
if(typeof ps[this.partial] !== 'function') | |
ps[this.partial] = handlebars.Handlebars.compile(ps[this.partial]); | |
return ps[this.partial](this); | |
} | |
}; | |
var onError = function (err) { | |
console.log(err); | |
}; | |
return gulp.src(config.paths.handlebars.templates) | |
.pipe(plumber( {errorHandler: onError})) | |
.pipe(handlebars(data, { | |
batch: config.paths.handlebars.partials, | |
helpers: helpers | |
})) | |
.pipe(ext.replace('html')) | |
.pipe(gulp.dest(config.paths.out.handlebars.templates)) | |
.pipe(bs.stream({once: true})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment