Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emerson-pereira/00260a60ab5dc5e073c443bc1536c8e8 to your computer and use it in GitHub Desktop.
Save emerson-pereira/00260a60ab5dc5e073c443bc1536c8e8 to your computer and use it in GitHub Desktop.
/**
* @desc Compile and execute handlebars and populate dest dir with markup
*/
gulp.task('handlebars', function() {
/**
* @desc File that contains data to be compile and executed by Handlebars
* @type {json}
*/
var handlebarsContext = require('./src/data.json');
/**
* @desc Compile handlebars template from specified path then execute with the data from the provided json source
* @param {string} err Error message
* @param {handlebars} source Handlebars template source
*/
function compileAndExecuteHandlebars(err, source) {
if (err) {
return console.log(err);
}
var template = handlebars.compile(source);
var markup = template(handlebarsContext);
console.log('********** Handlebars template compile and executed **********');
if (!fs.existsSync('./dist/')) {
fs.mkdirSync('./dist/');
}
populateResult(markup);
}
/**
* @desc Write handlebars result markup file into the specified dir
*/
function populateResult(markup) {
fs.writeFile('./dist/index.html', markup, (err) => {
if (err) throw err;
console.log('********** Markup file saved **********');
});
}
fs.readFile('./src/index.html', 'utf8', compileAndExecuteHandlebars);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment