Skip to content

Instantly share code, notes, and snippets.

@jhaynie
Forked from grantges/hbsHelpers.js
Last active August 29, 2015 14:15
Show Gist options
  • Save jhaynie/ad7e710a2aa9e563dc4c to your computer and use it in GitHub Desktop.
Save jhaynie/ad7e710a2aa9e563dc4c to your computer and use it in GitHub Desktop.
/**
* Helper Functions for Handlebars Templating engine
*/
var fs = require('fs'),
path = require('path'),
Arrow = require('arrow');
/**
* This function loops through a directory, and setups up all of the
* associated files as Handlebar Partials.
*
* @param dir {String} - Path of the diretory to use as partials directory
* @param callback {Function} - optional callback
*/
function registerPartials(dir, callback) {
/**
* Use the filesystem object to retrieve a list of the files in the
* passed in directory
*/
fs.readdir(dir, function(error, list) {
/** if there is an error, return the error value **/
if (error) {
return callback && callback(error);
}
/** Iterate through the list and assign the partial **/
list.forEach(function(fileName) {
if (path.extname(fileName)!=='.hbs') { return; }
var fn = _dir + '/' + fileName,
stat = fs.statSync(fn);
/** if this is a directory, then recurse through it **/
if (stat.isDirectory()) {
registerPartials(fn);
}
else if (stat.isFile())
var content = fs.readFileSync(fn, 'utf8');
Arrow.Middleware.getRendererEngine('hbs').registerPartial(fileName.replace(".hbs", ""), content)
});
callback && callback();
});
}
exports.registerPartials = registerPartials;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment