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
Thanks a lot. Loved it!
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside