Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 561
Sean O'Hara Avatar asked Dec 29 '25 22:12

Sean O'Hara


1 Answers

In handlebar:

{{span 'className'}}

In app:

Handlebars.registerHelper('span', function(className) {
  return new Handlebars.SafeString("<span class='"+Handlebars.Utils.escapeExpression(className)+"'></span>");
});
like image 83
lesyk Avatar answered Jan 01 '26 13:01

lesyk