Skip to main content

String Filters

Filters for transforming and manipulating text strings.

Case Transformation​

FilterDescription
upperConvert to uppercase
lowerConvert to lowercase
titleConvert to title case
capitalizeCapitalize 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​

FilterArgumentsDescription
trimcharacter_mask, sideRemove 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​

FilterArgumentsDescription
truncatelen, moreTrim 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​

FilterArgumentsDescription
excerptnum_words, moreTrim to word count
excerpt_charsnum_chars, moreTrim 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​

FilterArgumentsDescription
striptagsallowable_tagsRemove 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​

FilterArgumentsDescription
replacefrom, to or {map}Replace text
splitdelimiter, limitSplit into array
slugseparatorCreate URL-friendly slug
sanitizeseparatorAlias 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​

FilterArgumentsDescription
format...valuessprintf-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/ -->