Search code examples
ember.jshandlebars.jshelperdiscourse

How to create template helper


I want to create eq helper. It already exists in ember-truth-helpers addon, but I need only eq helper so I decided create it by myself in my plugin.

I've created file assets/javascripts/discourse/helpers/eq.js.es6 in my plugin with such content:

import { registerHelper } from 'discourse/lib/helpers';

registerHelper('eq', function(params) {
  return params[0] === params[1];
});

and use it in template in this way:

{{#if (eq param1 param2)}} <h1>hello</h1> {{/if}}

But eq is not defined.

What is the right way to create helper?


Solution

  • The problem was with the bounding. This code works for me:

    import { registerHelper } from 'discourse/lib/helpers';
    
    var makeBoundHelper = Ember.HTMLBars.makeBoundHelper;
    
    registerHelper('eq', makeBoundHelper(function(params) {
      return params[0] === params[1];
    }));
    

    The solution is taken from here