Meteor shorten string spacebar helper
If You're building some lists view (e.g. posts list) and You want to shorten their names You can achieve that using this UI helper:
UI.registerHelper('shortIt', function(stringToShorten, maxCharsAmount){
if(stringToShorten.length > maxCharsAmount){
return stringToShorten.substring(0, maxCharsAmount) + '...';
}
return stringToShorten;
});
Usage:
{{#each PostList}}
{{shortIt this.title 15}}
{{/each}}
And that's it! Enjoy!
Written by OfcaPL
Related protips
2 Responses
Thanks, this was helpful. I change it a bit to accommodate what I needed.
UI.registerHelper('shortIt', function(stringToShorten, maxCharsAmount){
if(stringToShorten.length <= maxCharsAmount){
return stringToShorten;
}
return stringToShorten.substring(0, maxCharsAmount);
});
I did this so that the returned string is shortened only if it is longer than what was put in and if it isn't I only needed the whole string returned.
Thanks
over 1 year ago
·
Thanks a lot. Loved it!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ui
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#