Trimming & Truncation

Wordsmith provides several helpful methods for trimming text content.

Wordsmith’s trimming & truncation methods include:

Truncation

chop

Chops content down to the given limit of paragraphs, words, or characters.

Will optionally append a string to the end of the chopped content.

Skips over allowedTags as instructed.

{% set text %}
<p>She sells seashells by the seashore.</p>
<p>The shells that she sells are seashells, I'm <em>sure</em>.</p>
<p>So, if she sells shells she sees on the shore,</p>
<p>Yes, I say she sells seashore shells, for sure.</p>
{% endset %}

{{ text | chop }}

# '<p>She sells seashells by the seashore.</p>'

{{ text | chop(limit=2, allowedTags="<em>") }}

# '<p>She sells seashells by the seashore.</p>
# <p>The shells that she sells are seashells, I'm <em>sure</em>.</p>'

{{ text | chop(limit=3, unit='w') }}

# 'She sells seashells'

{{ text | chop(limit=2, unit='c') }}

# 'Sh'

hacksaw

An alias for chop.

Provided for backwards compatibility, so that Wordsmith can be a drop-in replacement for the Hacksaw plugin.

Trimming

trim

Returns a string with whitespace removed from the start and end of the string. Supports the removal of unicode whitespace. Accepts an optional string of characters to strip instead of the defaults.

{{ '  fòôbàř  ' }}

# 'fòôbàř'

trimLeft

Returns a string with whitespace removed from the start of the string. Supports the removal of unicode whitespace. Accepts an optional string of characters to strip instead of the defaults.

{{ '  fòôbàř  ' }}

# 'fòôbàř '

trimRight

Returns a string with whitespace removed from the end of the string. Supports the removal of unicode whitespace. Accepts an optional string of characters to strip instead of the defaults.

{{ '  fòôbàř  ' }}

# ' fòôbàř'