I'd like to create a html code using lodash.template(), but the result was not I expected.
var a = '<td>a</td>';
var expected = '<tr><td>a</td></tr>';
var actual = _.template('<tr><%- a %></tr>', {a: a});
console.log(actual);
"<tr><td>a</td></tr>"
Populated text a
has been escaped as a result.
How can I get the result as expected
?
I can set options.escape
, but have no idea how to use this option.
Thanks!
You can use <%= a %>
or print
in evaluate context as documented, though generating HTML this way is not recommended for security.
var actual = _.template('<tr><% print(a) %></tr>', {a: a});
// or
var actual = _.template('<tr><%= a %></tr>', {a: a});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With