[英]How to pass a property to an Handlebars helper
I am trying to register a Handlebars.js helper in my Ember.js app that will allow me to pass in a view property that is a simple html string to be rendered without being escaped. 我试图在我的Ember.js应用程序中注册一个Handlebars.js帮助器,这将允许我传入一个视图属性,这是一个简单的html字符串,无需转义即可呈现。 My template looks like this: 我的模板看起来像这样:
<span class="add-on">{{log view.append}}{{safeMarkup view.append}}</span>
In this case the log statement outputs the html string properly to the console, something like <span>text</span>
. 在这种情况下,log语句将html字符串正确输出到控制台,类似于<span>text</span>
。
My helper, safeMarkup, is as follows: 我的助手safeMarkup如下:
Handlebars.registerHelper('safeMarkup', (string) ->
return new Handlebars.SafeString(string)
)
Yet, what gets rendered is not the value of the view.append
property but the string "view.append" itself! 然而,渲染的不是view.append
属性的值,而是字符串“view.append”本身! Like so: <span class="add-on">view.append</span>
. 像这样: <span class="add-on">view.append</span>
。 Any ideas what's going wrong here? 任何想法在这里出了什么问题? Thanks 谢谢
In handlebar: 在车把:
{{span 'className'}}
In app: 在app中:
Handlebars.registerHelper('span', function(className) {
return new Handlebars.SafeString("<span class='"+Handlebars.Utils.escapeExpression(className)+"'></span>");
});
您还可以使用三重胡子来避免转义html字符串... http://jsbin.com/imafuq/8/edit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.