最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I unescape html tags in lodash.template()? - Stack Overflow

programmeradmin3浏览0评论

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!

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!

Share Improve this question asked Jan 17, 2014 at 4:35 philipjkimphilipjkim 4,1497 gold badges39 silver badges49 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can use <%= a %> or print in evaluate context as documented, though generating HTML this way is not remended for security.

var actual = _.template('<tr><% print(a) %></tr>', {a: a});
// or
var actual = _.template('<tr><%= a %></tr>', {a: a});
发布评论

评论列表(0)

  1. 暂无评论