Skip to content

Commit

Permalink
Added exports.compile(str, options)
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 20, 2010
1 parent 138bcea commit 8a54850
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/jade.js
Expand Up @@ -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');
Expand All @@ -193,6 +193,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.
*
Expand Down
15 changes: 15 additions & 0 deletions test/jade.test.js
Expand Up @@ -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.