shorten

Truncates a string to at most the given number of bytes, returning it unchanged when it is already shorter.

The limit counts bytes, not characters, so cutting inside a multi-byte character can leave a broken final byte.

InputArgumentsReturnsstringintstringstringstringstring
Source

Behaviors

Coerces scalars to text
A scalar value such as a number or a boolean is converted to its text form before matching, so 42 is accepted as "42".
BasicSource

Truncates the value to at most the given number of bytes.

Given

{"text": "Hello world"}

Template

{{ text | shorten:5 }}

Result

Hello

Keeps just the first byte when the limit is one.

Given

{"text": "Hello"}

Template

{{ text | shorten:1 }}

Result

H
ScalarSource

Truncates a number by taking its string form first.

Given

{"n": 123456}

Template

{{ n | shorten:3 }}

Result

123
UnchangedSource

Returns the value as-is when it is already within the limit.

Given

{"text": "Hi"}

Template

{{ text | shorten:20 }}

Result

Hi
BasicSource

Accepts the length as a numeric string such as shorten:'5'.

Given

{"text": "Hello world"}

Template

{{ text | shorten:'5' }}

Result

Hello
UnchangedSource

Leaves the value as-is when it is shorter than a numeric-string limit.

Given

{"text": "Hi"}

Template

{{ text | shorten:'20' }}

Result

Hi