Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I unescape html tags in lodash.template()?

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>&lt;td&gt;a&lt;/td&gt;</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!

like image 555
philipjkim Avatar asked Mar 21 '23 22:03

philipjkim


1 Answers

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});
like image 180
tungd Avatar answered Apr 02 '23 05:04

tungd