Last active
August 29, 2015 14:15
-
-
Save grantges/3edbccb9d1e51d39982c to your computer and use it in GitHub Desktop.
Helper Functions for Handlebars Template Engine for use with the Appcelerator Arrow Middleware
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') | |
Arrow = require('arrow'), | |
watch = require('node-watch'); | |
/** | |
* This function loops through a directory, and setups up all of the | |
* associated files as Hanlebar Partials. | |
* | |
* @param _dir {String} - Path of the diretory to use as partials directory | |
* @param _watch {Boolean} - Should the function watch the director for changes | |
* and update | |
*/ | |
function registerPartials(_dir, _watch) { | |
_watch = _watch || false; | |
/** | |
* 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 error; | |
} | |
list.forEach(function(fileName) { | |
var path = _dir + '/' + fileName; | |
var stat = fs.statSync(path); | |
/** if this is a directory, then recurse through it **/ | |
if (stat.isDirectory()) { | |
registerPartials(path); | |
} else if (stat.isFile()) | |
var content = fs.readFileSync(path, 'utf8'); | |
Arrow.Middleware.getRendererEngine('hbs').registerPartial(fileName.replace(".hbs", ""), content) | |
}); | |
}); | |
/** | |
If the dev ops to watch the folder, then lets setup a recursive watch on the directory, and update | |
the partial accordingly | |
**/ | |
if(_watch){ | |
watch( _dir, function(filename) { | |
var content = fs.readFileSync(filename, 'utf8'); | |
var file = filename.split("/").pop(); | |
Arrow.Middleware.getRendererEngine('hbs').registerPartial(file.replace(".hbs", ""), content) | |
}); | |
} | |
} | |
exports.registerPartials = registerPartials; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment