mercredi 9 décembre 2015

Ember 2: Truncate text and add ellipses

I'm looking for a simple addon or helper for long strings that would truncate and also add ellipses. I found some examples with handlebar helper but I think most are outdated. I created an ember helper called truncate-text and tried to piece together examples, but is unsuccessful. Also, if there's a way to define a limit in number of character per use cases, that would be a bonus.

Here is what I have in my helpers/truncate-text.js import Ember from 'ember';

export function truncateText(text) {
  if (text.length > 60) {
    var theString = text.substring(0, 60) + " ... ";
    return new Ember.Handlebars.SafeString(theString);
  } else {
    return text;
  }
}

export default Ember.Helper.helper(truncateText);

In my template.hbs {{truncate-text text="Long text here."}

Would be grateful if I can do this {{truncate-text text="Long text here." limit=65}}




Aucun commentaire:

Enregistrer un commentaire