Skip to content

Commit

Permalink
Fixed #94 correctly specify the execution context
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Jun 29, 2015
1 parent 8cdbee3 commit 727c527
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ exports.resolveInclude = function(name, filename) {
function handleCache(options, template) {
var fn
, path = options.filename
, hasTemplate = template !== undefined;
, hasTemplate = arguments.length > 1;

if (options.cache) {
if (!path) {
Expand Down Expand Up @@ -312,8 +312,7 @@ exports.render = function (template, data, opts) {
cpOptsInData(data, opts);
}

fn = handleCache(opts, template);
return fn.call(opts.context, data);
return handleCache(opts, template)(data);
};

/**
Expand All @@ -337,6 +336,9 @@ exports.renderFile = function () {
, opts = args.pop() || {}
, result;

// Don't pollute passed in opts obj with new vals
opts = utils.shallowCopy({}, opts);

// No options object -- if there are optiony names
// in the data, copy them to options
if (arguments.length == 3) {
Expand Down Expand Up @@ -378,6 +380,7 @@ function Template(text, opts) {
options.filename = opts.filename;
options.delimiter = opts.delimiter || exports.delimiter || _DEFAULT_DELIMITER;
options._with = typeof opts._with != 'undefined' ? opts._with : true;
options.context = opts.context;
options.cache = opts.cache || false;
options.rmWhitespace = opts.rmWhitespace;
this.opts = options;
Expand Down Expand Up @@ -474,6 +477,7 @@ Template.prototype = {

// Return a callable function which will execute the function
// created by the source-code, with the passed data as locals
// Adds a local `include` function which allows full recursive include
var returnedFn = function (data) {
var include = function (path, includeData) {
var d = utils.shallowCopy({}, data);
Expand All @@ -482,7 +486,7 @@ Template.prototype = {
}
return includeFile(path, opts)(d);
};
return fn(data || {}, escape, include, rethrow);
return fn.apply(opts.context, [data || {}, escape, include, rethrow]);
};
returnedFn.dependencies = this.dependencies;
return returnedFn;
Expand Down
19 changes: 18 additions & 1 deletion test/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ suite('ejs.compile(str, options)', function () {
});
});

suite('ejs.render(str, data)', function () {
suite('ejs.render(str, data, opts)', function () {
test('render the template', function () {
assert.equal(ejs.render('<p>yay</p>'), '<p>yay</p>');
});
Expand Down Expand Up @@ -228,6 +228,11 @@ suite('ejs.render(str, data)', function () {
ejs.cache = oldCache;
});

test('opts.context', function () {
var ctxt = {foo: 'FOO'}
, out = ejs.render('<%= this.foo %>', {}, {context: ctxt});
assert.equal(out, ctxt.foo);
});
});

suite('ejs.renderFile(path, [data], [options], fn)', function () {
Expand Down Expand Up @@ -342,6 +347,18 @@ suite('ejs.renderFile(path, [data], [options], fn)', function () {
});
});

test('opts.context', function (done) {
var ctxt = {foo: 'FOO'};
ejs.renderFile('test/fixtures/with-context.ejs', {},
{context: ctxt}, function(err, html) {
if (err) {
return done(err);
}
assert.equal(html, ctxt.foo + '\n');
done();
});

});
});

suite('cache specific', function () {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/with-context.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= this.foo %>

0 comments on commit 727c527

Please sign in to comment.