Ben Davis
Ben Davis

Reputation: 13780

How do I pass a variable to a handlebars helper?

I'm trying to implement a simple "if-equal" helper in handlebars, like so:

Ember.Handlebars.registerHelper('ifeq', function(val1, val2, options) {
    return (val1 == val2) ? options.fn(this) : '';
});

And use it like so (let's assume the foo variable is set to "bar"):

{{#ifequal foo "bar"}} show something {{/ifequal}}

The problem is, when val1 is passed in, it's passed in as the string "foo", and not the actual value of the foo variable. In my debugger, I can verify that this[val1] is in fact set to the expected value of the foo variable.

Does Ember somehow alter the behavior?

Upvotes: 1

Views: 3914

Answers (1)

Bradley Priest
Bradley Priest

Reputation: 7458

Ember.Handlebars.registerHelper just passes strings in. There is a registerBoundHelper being worked on, but in your case this should work.

Ember.Handlebars.registerHelper('ifeq', function(val1, val2, options) {
   return (this.get(val1) == val2) ? options.fn(this) : '';
});

Upvotes: 4

Related Questions