How to get an excerpt of a key?


#1

Hey,

As the title says I’m looking for a way to get an excerpt (like first 100 characters for example) of text instead of the full thing. Currently I have this in my template:

{{#each note}}
    <li class="note {{selectedClass}}">
        <strong>{{name}}</strong>
        <p>{{text}}</p>
        <span class="lastmodified"  title="Last modified">{{noteLastModified lastModified}}</span>
    </li>
{{/each}}

Do I need to make a helper for the text or is there another way?


#2

I have this global helpers defined in all my applications since getting shorted text is always helpful. It is a helper function that takes a var or other helper and a length and truncates it for you. (default is 15 characters)

Template.registerHelper('shorten',
  function(str, len, kwargs) {
    str = str ? str : ''
    len = Util.isInt(len) ? len : 15
    return str.trunc(len);
  }
);

You can use it like this.

<p>{{shorten text 30}}</p>

or (which defaults to 15 characters)

<p>{{shorten text}}</p>