String Filters
Filters for transforming and manipulating text strings.
Case Transformation
| Filter | Description |
|---|---|
upper | Convert to uppercase |
lower | Convert to lowercase |
title | Convert to title case |
capitalize | Capitalize first letter, lowercase rest |
{{ post.title|upper }} <!-- HELLO WORLD -->
{{ post.title|lower }} <!-- hello world -->
{{ post.title|title }} <!-- Hello World -->
{{ post.title|capitalize }} <!-- Hello world -->
Trimming
| Filter | Arguments | Description |
|---|---|---|
trim | chars, side | Remove characters from string |
Arguments:
chars— Characters to trim (default: whitespace)side—'both'(default),'left', or'right'
{{ post.title|trim }} <!-- Remove whitespace -->
{{ post.title|trim('/') }} <!-- Remove slashes -->
{{ post.title|trim(' ', 'left') }} <!-- Trim left only -->
{{ post.title|trim(' ', 'right') }} <!-- Trim right only -->
Truncation
| Filter | Arguments | Description |
|---|---|---|
truncate | length, suffix, preserve | Limit string length |
Arguments:
length— Maximum characters (default: 80)suffix— Append to truncated text (default:'...')preserve— Preserve whole words (default:true)
{{ post.excerpt|truncate(100) }}
{{ post.excerpt|truncate(100, '...') }}
{{ post.excerpt|truncate(100, '...', true) }}
{{ post.excerpt|truncate(100, '', false) }}
Excerpt
| Filter | Arguments | Description |
|---|---|---|
excerpt | words, more | Trim to word count |
excerpt_chars | chars, more | Trim to character count |
excerpt Arguments:
words— Number of words (default: 50)more— Suffix text (default:'…')
excerpt_chars Arguments:
chars— Number of characters (default: 60)more— Suffix text (default:'…')
{{ post.content|excerpt }} <!-- 50 words -->
{{ post.content|excerpt(25) }} <!-- 25 words -->
{{ post.content|excerpt(25, '...') }}
{{ post.content|excerpt_chars(200) }}
{{ post.content|excerpt_chars(200, '...') }}
HTML Processing
| Filter | Arguments | Description |
|---|---|---|
striptags | allowed | Remove HTML tags |
nl2br | — | Convert newlines to <br> tags |
spaceless | — | Remove whitespace between HTML tags |
pretags | — | Escape HTML inside <pre> tags |
{{ post.content|striptags }} <!-- Remove all tags -->
{{ post.content|striptags('<p><a>') }} <!-- Keep p and a tags -->
{{ text|nl2br }} <!-- Newlines to <br> -->
{{ html|spaceless }} <!-- Remove tag whitespace -->
Text Manipulation
| Filter | Arguments | Description |
|---|---|---|
replace | search, replace or {map} | Replace text |
split | delimiter, limit | Split into array |
slug | separator | Create URL-friendly slug |
sanitize | separator | Alias for slug |
replace formats:
- Simple:
replace('old', 'new') - Map:
replace({'%name%': 'World', '%age%': '30'})
{{ post.title|replace('old', 'new') }}
{{ post.title|replace({'%name%': user.name}) }}
{{ post.tags|split(',') }}
{{ post.tags|split(',', 3) }}
{{ post.title|slug }} <!-- hello-world -->
{{ post.title|slug('_') }} <!-- hello_world -->
Formatting
| Filter | Arguments | Description |
|---|---|---|
format | ...values | sprintf-style formatting |
url_encode | — | URL encode string |
relative | — | Convert absolute URL to relative |
{{ '%s has %d posts'|format(user.name, count) }}
{{ search_query|url_encode }}
{{ post.link|relative }} <!-- /2024/01/post-slug/ -->