简体   繁体   中英

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. 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> .

My helper, safeMarkup, is as follows:

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! Like so: <span class="add-on">view.append</span> . Any ideas what's going wrong here? Thanks

In handlebar:

{{span 'className'}}

In app:

Handlebars.registerHelper('span', function(className) {
  return new Handlebars.SafeString("<span class='"+Handlebars.Utils.escapeExpression(className)+"'></span>");
});

您还可以使用三重胡子来避免转义html字符串... http://jsbin.com/imafuq/8/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM