-
-
Save jhaynie/ad7e710a2aa9e563dc4c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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