O'Reilly logo

Mastering Ember.js by Mitchel Kelonye

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Defining custom helpers

Handlebars provides ways to create your own helpers. The following is the format used to register new helpers:

Ember.Handlebars.register(helper_name, helper_function_or_class);

For example, let's create a heading helper that creates h1 tags:

  Ember.Handlebars.register('heading', function(text, options){
    var escapedText = Handlebars.Utils.escapeExpression(text);
     var heading = '<h1>'+escapedText+'</h1>';
               return new Handlebars.SafeString(heading);
  });

This can then be used in our application templates as:

  {{heading 'Title'}}

Now, yield the following:

  <h1>Title</h1>

The following example also demonstrates how to create helpers from existing views:

    Ember.Handlebars.register('loader', App.LoaderView}}

This can now be used simply as: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required