Forked from gulp-template Inspired by grunt-compile-handlebars
Compile Handlebars templates
Install with npm
npm install --save-dev gulp-compile-handlebars
var gulp = require('gulp');
var handlebars = require('gulp-compile-handlebars');
var rename = require('gulp-rename');
gulp.task('default', function () {
var templateData = {
firstName: 'Kaanon'
},
options = {
ignorePartials: true, //ignores the unknown footer2 partial in the handlebars template, defaults to false
partials : {
footer : '<footer>the end</footer>'
},
batch : ['./src/partials'],
helpers : {
capitals : function(str){
return str.toUpperCase();
}
}
}
return gulp.src('src/hello.handlebars')
.pipe(handlebars(templateData, options))
.pipe(rename('hello.html'))
.pipe(gulp.dest('dist'));
});
<h1>Header</h1>
<p>Hello Kaanon</p>
<p>HELLO! KAANON</p>
<footer>the end</footer>
- ignorePartials : ignores any unknown partials. Useful if you only want to handle part of the file
- partials : Javascript object that will fill in partials using strings
- batch : Javascript array of filepaths to use as partials
- helpers: javascript functions to stand in for helpers used in the handlebars files
- compile: compile options. See handlebars reference for possible values
- debugMode: enable debug mode when including partials. This can be useful when using as a static site generator. Ignored if not set.
- debugMode.start: HTML string to use at the beginning of a partial. Specifiy a string (preferably a HTML comment).
{{partial}}
will be replaced with current partial name. - debugMode.end: HTML string to use at the end of a partial. Specifiy a string (preferably a HTML comment).
{{partial}}
will be replaced with current partial name. - debugMode.logContext: HTML string to output the current context of a partial. Specify a string (preferably a HTML comment) which will be placed before the actual partial content.
{{context}}
will be replaced with current context as JSON.
- debugMode.start: HTML string to use at the beginning of a partial. Specifiy a string (preferably a HTML comment).
You can access the Handlebars library from the handlebars.Handlebars
property.
var handlebars = require('gulp-compile-handlebars');
var safestring = new handlebars.Handlebars.SafeString('<strong>HELLO! KAANON</strong>');
Use gulp-data to pass a data object to the template based on the handlebars file being processed. If you pass in template data this will be extended with the object from gulp-data.
See gulp-data for usage examples.