Created
June 1, 2015 14:59
-
-
Save jadnco/7792a6fa41dd9e8e9bc2 to your computer and use it in GitHub Desktop.
Handlebars truncate helper method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '--'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
In Handlebars: