Better backward compat, docs

This commit is contained in:
mde
2015-01-04 09:54:45 -08:00
parent 3b557c47a6
commit 186a522e90
2 changed files with 21 additions and 5 deletions

View File

@@ -40,6 +40,10 @@ ejs.render(str, data, options);
// => Renderded HTML string
```
You can also use the shortcut `ejs.render(dataAndOptions);` where you pass
everything in a single object. In that case, you'll end up with local variables
for all the passed options.
## Options
- `cache` Compiled functions are cached, requires `filename`
@@ -48,6 +52,7 @@ ejs.render(str, data, options);
- `compileDebug` When `false` no debug instrumentation is compiled
- `client` Returns standalone compiled function
- `delimiter` Character to use with angle brackets for open/close
- `debug` Output generated function body
## Tags

View File

@@ -26,10 +26,12 @@ var utils = require('./utils')
, rethrow
, _DEFAULT_DELIMITER = '%'
, _REGEX_STRING = '(<%%)|(<%=)|(<%-)|(<%#)|(<%)|(%>)|(-%>)'
, _OLD_OPTS = {
, _OPTS_MAP = {
cache: 'cache'
, filename: 'filename'
, delimiter: 'delimiter'
, scope: 'context'
, context: 'context'
, debug: 'debug'
, compileDebug: 'compileDebug'
, client: 'client'
@@ -138,17 +140,22 @@ exports.render = function () {
, data = args.shift() || {}
, opts = args.shift() || {};
// v1 compat
// No options object -- if there are optiony names
// in the data, copy them to options
// FIXME: Remove this in a future version
if (arguments.length == 2) {
for (var p in _OLD_OPTS) {
for (var p in _OPTS_MAP) {
if (typeof data[p] != 'undefined') {
opts[_OLD_OPTS[p]] = data[p];
opts[_OPTS_MAP[p]] = data[p];
}
}
}
// v1 compat
// 'scope' is 'context'
// FIXME: Remove this in a future version
if (opts.scope) {
opts.context = opts.scope;
delete opts.context;
}
if (opts.cache) {
if (!opts.filename) {
@@ -272,6 +279,10 @@ Template.prototype = new function () {
src = this.source;
}
if (opts.debug) {
console.log(src);
}
if (opts.client) {
src = 'escape = escape || ' + escape.toString() + ';\n' + src;
src = 'rethrow = rethrow || ' + rethrow.toString() + ';\n' + src;