Changelog
5.1.3
Changelog
5.1.2
stricMode
not working for compile.standAlone
Changelog
5.1.1
<% %>
)<my-tag><%= a %> text</my-tag>
)Changelog
5.0.0
reduce(tokens[, compileDebug])
to reduce(tokens[, options])
with(locals)
by default.The old version (v4) compiled templates to sloppy JS mode and used the long-deprecated with()
structure.
The new (v5) version changes that, but allows one to opt-out.
We used with(locals)
to allow one to write <%= x %>
instead of <%= locals.x %>
, but employing with()
forced
the lib to compile to sloppy mode since this construct isn't allowed in strict mode.
In this new version, we revisited that decision and prefered to drop with
in favor of strict mode.
To ease transition, you can opt-out and keep using the old behavior with the option strictMode: false
.
On the other hand, if don't want to keep writing locals.
all the time, you can list which variables should be made
available with the option vars: ['someVar', 'anotherOne']
. See the examples below
/**
* Old (v4)
*/
// Variables could be accessed directly, unless there were absent in the locals parameter
ejs.render('<%= a %>', {a: 2}) // '2'
ejs.render('<%= b %>', {a: 2}) // ReferenceError: b is not defined
// In sloppy mode, weird things happen, like leaking to global context
ejs.render('<% x = 17 %>') // ''
x // 17
/**
* New (v5)
*/
// Direct access does not work out of the box
ejs.render('<%= a %>', {a: 2}) // ReferenceError: a is not defined
// You have to be explicit. Either use the locals object or list variables to be made available
ejs.render('<%= locals.a %>', {a: 2}) // '2'
ejs.render('<%= a %>', {a: 2}, {vars: ['a']}) // '2'
ejs.render('<%= b %>', {a: 2}, {vars: ['b']}) // ''
// Code is executed in strict mode
ejs.render('<% x = 17 %>') // ReferenceError: x is not defined
// If you REALLY want to use old behavior
ejs.render('<%= a %>', {a: 2}, {strictMode: false}) // '2'
strictMode
(defaults to true
)vars
(defaults to []
)Changelog
4.0.3
Changelog
4.0.2
compileDebug
set to false
would crash on runtime. The fix on 4.0.1 did not covered the recursive case.Changelog
4.0.1
compileDebug
set to false
would crash on runtime