Browse files

Added renderFile

  • Loading branch information...
1 parent 382bc11 commit 2a8d8bea607ccc20b329614cd7bf29acf4de5780 @cjoudrey cjoudrey committed Feb 5, 2011
Showing with 33 additions and 0 deletions.
  1. +33 −0 lib/haml.js
View
33 lib/haml.js
@@ -6,6 +6,7 @@
*/
var sys = require('sys')
+var fs = require('fs')
/**
* Version.
@@ -664,3 +665,35 @@ 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 = true
+
+ if (!exports.cache[filename]) {
+ fs.readFile(filename, encoding, function(err, str) {
+ if (err) {
+ callback(err)
+ } else {
+ callback(null, exports.render(str, options))
+ }
+ });
+ } else {
+ process.nextTick(function() {
+ callback(null, exports.render(null, options))
+ });
+ }
+}

0 comments on commit 2a8d8be

Please sign in to comment.