在Ember助手中使用Unescape HTML

Ken*_*yer 4 ember.js

我的Ember应用程序中有以下帮助:

Ember.Handlebars.helper "social_profiles", ((person) ->
  person.social_profiles.map (item) ->
    " <a href=''> #{item.type_name}</a>"
), "social_profiles"
Run Code Online (Sandbox Code Playgroud)

每次我调用帮助器时它都会返回一个转义字符串,但是我想要ember来显示HTML链接.

我怎样才能做到这一点?

Rya*_*yan 9

您可以将字符串标记为安全new Handlebars.SafeString("<b>hello world</b>").把手现在不会逃脱任何输入.

但是,您需要确保您的字符串是安全的.由于您传入的item.type_name内容可能包含无法捕获的恶意代码,因为您声明该字符串是安全的.

为了解决这个问题,首先要转义用户输入,然后将其包装在标记为安全的标签中.

例:

Ember.Handlebars.registerHelper('boldItem', function(item) {
  var escaped = Handlebars.Utils.escapeExpression(item);
  return new Handlebars.SafeString("<b>" + escaped + "</b>");
});
Run Code Online (Sandbox Code Playgroud)