Forked from TastyToast/handlebars-truncate-helper.js
Last active
August 29, 2015 14:09
-
-
Save Minasokoni/f4f05f566574421cd94d to your computer and use it in GitHub Desktop.
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 (str, len) { | |
if (str.length > len) { | |
var new_str = str.substr (0, len+1); | |
while (new_str.length) { | |
var ch = new_str.substr ( -1 ); | |
new_str = new_str.substr ( 0, -1 ); | |
if (ch == ' ') { | |
break; | |
} | |
} | |
if ( new_str == '' ) { | |
new_str = str.substr ( 0, len ); | |
} | |
return new Handlebars.SafeString ( new_str +'...' ); | |
} | |
return str; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment