Skip to content
Browse files

Added exports.compile(str, options)

  • Loading branch information...
1 parent 138bcea commit 8a548509d90199440816db30fc5de0cf92d2c1ed @tj tj committed
Showing with 35 additions and 3 deletions.
  1. +20 −3 lib/jade.js
  2. +15 −0 test/jade.test.js
View
23 lib/jade.js
@@ -179,10 +179,10 @@ function parse(str, options){
try {
return ''
- + attrs.toString()
- + escape.toString()
+ + attrs.toString() + '\n\n'
+ + escape.toString() + '\n\n'
+ 'var buf = [];\n'
- + 'with (locals) {' + js + '}'
+ + 'with (locals || {}) {' + js + '}'
+ 'return buf.join("");';
} catch (err) {
process.compile(js, filename || 'Jade');
@@ -194,6 +194,23 @@ function parse(str, options){
}
/**
+ * Compile a `Function` representation of the given jade `str`.
+ *
+ * @param {String} str
+ * @param {Options} options
+ * @return {Function}
+ * @api public
+ */
+
+exports.compile = function(str, options){
+ var fn = [
+ 'var _ = { lineno: 1 };',
+ parse(String(str), options || {})
+ ].join('\n');
+ return new Function('locals', fn);
+};
+
+/**
* Render the given `str` of jade.
*
* Options:
View
15 test/jade.test.js
@@ -837,5 +837,20 @@ module.exports = {
beforeExit(function(){
assert.equal(1, called);
});
+ },
+
+ 'test .compile()': function(assert){
+ var fn = jade.compile('p foo');
+ assert.equal('<p>foo</p>', fn());
+ },
+
+ 'test .compile() locals': function(assert){
+ var fn = jade.compile('p= foo');
+ assert.equal('<p>bar</p>', fn({ foo: 'bar' }));
+ },
+
+ 'test .compile() scope': function(assert){
+ var fn = jade.compile('p= this.foo');
+ assert.equal('<p>bar</p>', fn.call({ foo: 'bar' }));
}
};

0 comments on commit 8a54850

Please sign in to comment.
Something went wrong with that request. Please try again.