Last Updated: February 25, 2016
·
2.569K
· ofcapl

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!

2 Responses
Add your response

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
Did you know?

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