Skip to content

Instantly share code, notes, and snippets.

@jadnco
Created June 1, 2015 14:59
Show Gist options
  • Save jadnco/7792a6fa41dd9e8e9bc2 to your computer and use it in GitHub Desktop.
Save jadnco/7792a6fa41dd9e8e9bc2 to your computer and use it in GitHub Desktop.
Handlebars truncate helper method
Handlebars.registerHelper('truncate', function(length, value, opts) {
// Make sure value exists
if (value != null) {
// Don't truncate if already short
if (value.length <= length) {
return value;
}
// Truncate to length and add ellipsis
return value.substring(0, length) + '...';
}
return '--';
});
@jadnco
Copy link
Author

jadnco commented Jun 1, 2015

Usage

In Handlebars:

{{truncate 10 'Hello World'}} {{! Output: Hello Worl... }}
{{truncate 12 'Hello World'}} {{! Output: Hello World }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment