Skip to content

Instantly share code, notes, and snippets.

@hoodwink73
Created January 23, 2016 14:07
Show Gist options
  • Save hoodwink73/32058a141e4966c08901 to your computer and use it in GitHub Desktop.
Save hoodwink73/32058a141e4966c08901 to your computer and use it in GitHub Desktop.
Some common handlebars helpers
Handlebars.registerHelper('trimTextWithReadMoreLink', function (textToTrim) {
var link, textToShow, el;
var wordsAllowed = 30;
if ( textToTrim === undefined) {
return "";
}
var wordsTokenized = textToTrim.split(' ');
if (wordsTokenized.length > wordsAllowed) {
link = '<a href="#" class="see-more">See More</a>';
textToShow = wordsTokenized.slice(0, wordsAllowed + 1).join(' ');
el = [
'<span class="truncated-text-wrapper" data-original-text="', textToTrim, '">',
textToShow,
'... ',
link,
'</span>'
];
el = el.join('');
} else {
el = textToTrim;
}
return new Handlebars.SafeString(el);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment