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 | character_mask, side | Remove characters from string |
Arguments:
character_mask— 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 | len, more | Trim to word count, keeping some tags |
Arguments:
len— Number of words (default: 55)more— Appended when the text is cut (default:'…')
{{ post.excerpt|truncate(20) }} <!-- First 20 words… -->
{{ post.content|truncate(30) }} <!-- Keeps paragraphs and links -->
{{ post.title|truncate(5, '...') }}
truncate counts words, as in Timber. It keeps the p, a, span, b, i, br and blockquote tags, removes the others and closes the ones a cut leaves open, without ever cutting inside a tag. To count characters, use excerpt_chars; to remove every tag, excerpt.
Excerpt
| Filter | Arguments | Description |
|---|---|---|
excerpt | num_words, more | Trim to word count |
excerpt_chars | num_chars, more | Trim to character count |
excerpt Arguments:
num_words— Number of words (default: 55, as in WordPress)more— Suffix text (default:'…')
excerpt_chars Arguments:
num_chars— Number of characters (default: 60)more— Suffix text (default:'…')
{{ post.content|excerpt }} <!-- 55 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 | allowable_tags | 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>')|kses_post }} <!-- Keep p and a tags -->
{{ text|nl2br }} <!-- Newlines to <br> -->
{{ post.content|spaceless }} <!-- Remove tag whitespace -->
striptags returns text, escaped like any value, as in Twig: the tags you keep print as text unless you end with |kses_post.
nl2br, spaceless and pretags output HTML. A text value is escaped before them, so its own tags stay text: {{ post.meta('bio')|nl2br }} adds <br> without letting HTML from the field through. spaceless and pretags therefore only have tags to work on in a field that outputs HTML, such as post.content.
Text Manipulation
| Filter | Arguments | Description |
|---|---|---|
replace | from, to 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}) }}
{{ 'red,green,blue'|split(',')|join(' / ') }} <!-- red / green / blue -->
{{ 'one,two,three,four'|split(',', 3)|last }} <!-- three,four -->
{{ 'abc'|split('')|join(' ') }} <!-- a b c: one item per character -->
{{ 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/ -->