Sean O'Hara
Sean O'Hara

Reputation: 1228

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

Upvotes: 3

Views: 1500

Answers (2)

selvagsz
selvagsz

Reputation: 3872

You can also use triple-mustache to avoid escaping of the html string... http://jsbin.com/imafuq/8/edit

Upvotes: 0

lesyk
lesyk

Reputation: 4129

In handlebar:

{{span 'className'}}

In app:

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

Upvotes: 2

Related Questions