diff --git a/README.md b/README.md index 81a9aa10..85b56e77 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ You should never give end-users unfettered access to the EJS render method, If y previously resolved path. Should return an object `{ filename, template }`, you may return only one of the properties, where `filename` is the final parsed path and `template` is the included content. + - `functionClass` Custom [function constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function) replacement for a scenario where you would want to add hooks or provide a different execution backend. This project uses [JSDoc](http://usejsdoc.org/). For the full public API documentation, clone the repository and run `jake doc`. This will run JSDoc diff --git a/lib/ejs.js b/lib/ejs.js index 65590eae..83b4776e 100755 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -535,6 +535,7 @@ function Template(text, opts) { options.async = opts.async; options.destructuredLocals = opts.destructuredLocals; options.legacyInclude = typeof opts.legacyInclude != 'undefined' ? !!opts.legacyInclude : true; + options.functionClass = typeof opts.functionClass === 'function' ? opts.functionClass : undefined; if (options.strict) { options._with = false; @@ -652,7 +653,9 @@ Template.prototype = { } try { - if (opts.async) { + if (opts.functionClass !== undefined) { + ctor = opts.functionClass; + } else if (opts.async) { // Have to use generated function for this, since in envs without support, // it breaks in parsing try { diff --git a/test/ejs.js b/test/ejs.js index 607b4a71..34a65f3b 100755 --- a/test/ejs.js +++ b/test/ejs.js @@ -204,6 +204,15 @@ suite('ejs.compile(str, options)', function () { assert.ok(func.name === 'anonymous'); return done(); }); + + test('allow custom function ctor', function () { + var fn = ejs.compile('', { + functionClass: function(){ + return function(){ return 'potato'; } + } + }); + assert.equal(fn(), 'potato'); + }); }); suite('client mode', function () {