Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Added renderFile #31

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+32 −0
Split
View
@@ -6,6 +6,7 @@
*/
var sys = require('sys')
+var fs = require('fs')
/**
* Version.
@@ -664,3 +665,34 @@ exports.render = function(str, options) {
}
}).call(options.context)
}
+
+/**
+ * Render a file containing haml and cache the parser.
+ *
+ * @param {string} filename
+ * @param {string} encoding
+ * @param {object} options
+ * @param {function} callback
+ * @return {void}
+ * @api public
+ */
+
+exports.renderFile = function(filename, encoding, options, callback) {
+ options = options || {}
+ options.filename = options.filename || filename
+ options.cache = options.hasOwnProperty('cache') ? options.cache : true
+
+ if (exports.cache[filename]) {
+ process.nextTick(function() {
+ callback(null, exports.render(null, options))
+ });
+ } else {
+ fs.readFile(filename, encoding, function(err, str) {
+ if (err) {
+ callback(err)
+ } else {
+ callback(null, exports.render(str, options))
+ }
+ });
+ }
+}