How to get an excerpt of a key?

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?

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>
1 Like