Created
January 23, 2016 14:07
-
-
Save hoodwink73/32058a141e4966c08901 to your computer and use it in GitHub Desktop.
Some common handlebars helpers
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('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