Articles in this section

truncateWords helper

The truncateWords helper shortens a string to a specified number of words and optionally appends a suffix. Use it to limit text length in summaries, previews, or any field with a word count constraint.

Syntax

{{truncateWords string limit suffix}}

string -- The string to truncate. Required.

limit -- The number of words to keep. Required. Must be a non-negative integer. 0 returns only the suffix. Negative or non-numeric values return an empty string.

suffix -- The string to append after truncation. Optional. Defaults to …. Pass "" to truncate with no suffix.

Examples

Truncate to different word counts

{{truncateWords record.description 1}}
{{truncateWords record.description 2}}
{{truncateWords record.description 3}}

Input:

{ "record": { "description": "foo bar baz" } }

Output:

foo…
foo bar…
foo bar baz

When the word count is equal to or less than the total words in the string, the string is returned unchanged with no suffix.

Use a custom suffix

{{truncateWords record.notes 2 "--more"}}

Input:

{ "record": { "notes": "Integration setup requires testing" } }

Output: Integration setup--more

Truncate to zero words

{{truncateWords record.description 0}}

Input:

{ "record": { "description": "foo bar baz" } }

Output: …

A limit of 0 returns only the suffix.

Usage notes

  • Truncation respects word boundaries -- no partial words are returned.
  • If the word count is less than or equal to limit, the string is returned unchanged with no suffix appended.
  • A limit of 0 returns only the suffix. A negative or non-numeric limit returns an empty string.