Problem

You want to truncate string to a specified length inside your Handlebars templates.

Solution

Write a custom Handlebars helper that can truncates text.

Discussion

The Handlebars helper is very simple. It takes two parameters: str (string) and len (length).

  1. Ember.Handlebars.helper('truncate', function(str, len) {
  2. if (str.length > len) {
  3. return str.substring(0, len - 3) + '...';
  4. } else {
  5. return str;
  6. }
  7. });

Example

JS Bin