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.
InputArgumentsReturns
SourcestringintstringstringstringstringBehaviors
- Coerces scalars to text
- A scalar value such as a number or a boolean is converted to its text form before matching, so
42is accepted as"42".
BasicSource
Truncates the value to at most the given number of bytes.
Given
{"text": "Hello world"}Template
{{ text | shorten:5 }}Result
HelloOneSource
Keeps just the first byte when the limit is one.
Given
{"text": "Hello"}Template
{{ text | shorten:1 }}Result
HScalarSource
Truncates a number by taking its string form first.
Given
{"n": 123456}Template
{{ n | shorten:3 }}Result
123UnchangedSource
Returns the value as-is when it is already within the limit.
Given
{"text": "Hi"}Template
{{ text | shorten:20 }}Result
HiBasicSource
Accepts the length as a numeric string such as shorten:'5'.
Given
{"text": "Hello world"}Template
{{ text | shorten:'5' }}Result
HelloUnchangedSource
Leaves the value as-is when it is shorter than a numeric-string limit.
Given
{"text": "Hi"}Template
{{ text | shorten:'20' }}Result
Hi