Skip to content

Instantly share code, notes, and snippets.

@nsbingham
Last active December 10, 2015 07:09
Show Gist options
  • Save nsbingham/4399237 to your computer and use it in GitHub Desktop.
Save nsbingham/4399237 to your computer and use it in GitHub Desktop.
A simple helper to truncate values in Handlebars based on a character count.
Handlebars.registerHelper('trunc', function (context, options) {
var chars = options.hash.chars || 20;
var result = context;
if (context.length > chars) {
result = result.substring(0, chars);
if (options.hash.append) {
result += options.hash.append;
}
}
return new Handlebars.SafeString(result);
});
// Usage: foo = "abcdefghijklmnopqrstuvwxyz"
// {trunc foo chars="4" append="..."}
// Returns: abcd...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment