简体   繁体   中英

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?

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) : '';
});

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