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
trimchars, sideRemove 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

FilterArgumentsDescription
truncatelength, suffix, preserveLimit 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

FilterArgumentsDescription
excerptwords, moreTrim to word count
excerpt_charschars, moreTrim 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

FilterArgumentsDescription
striptagsallowedRemove HTML tags
nl2brConvert newlines to <br> tags
spacelessRemove whitespace between HTML tags
pretagsEscape 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

FilterArgumentsDescription
replacesearch, replace 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}) }}
{{ post.tags|split(',') }}
{{ post.tags|split(',', 3) }}
{{ post.title|slug }} <!-- hello-world -->
{{ post.title|slug('_') }} <!-- hello_world -->

Formatting

FilterArgumentsDescription
format...valuessprintf-style formatting
url_encodeURL encode string
relativeConvert absolute URL to relative
{{ '%s has %d posts'|format(user.name, count) }}
{{ search_query|url_encode }}
{{ post.link|relative }} <!-- /2024/01/post-slug/ -->