Skip to main content

Filters: Text and Strings

Text​

upper​

Converts a string to uppercase:

upper
{{ 'hello'|upper }}

lower​

Converts a string to lowercase:

lower
{{ 'HELLO'|lower }}
{# hello #}

capitalize​

Capitalizes the first letter:

capitalize
{{ 'hello world'|capitalize }}
{# Hello world #}

title​

Capitalizes the first letter of each word:

title
{{ 'hello world'|title }}
{# Hello World #}

trim​

Removes whitespace or specified characters from both ends:

trim
{{ ' hello '|trim }}
{# hello #}

{{ '---hello---'|trim('-') }}
{# hello #}

{{ ' hello '|trim(' ', 'left') }}

nl2br​

Replaces line breaks with <br>:

nl2br
{{ "line 1\nline 2"|nl2br }}
{# line 1<br />\nline 2 #}

spaceless​

Removes whitespace between HTML tags:

spaceless
{{ '<div> <p> text </p> </div>'|spaceless }}
{# <div><p> text </p></div> #}

striptags​

Removes HTML/XML tags:

striptags
{{ '<p>Hello <b>World</b></p>'|striptags }}
{# Hello World #}

{{ html|striptags('<br><p>') }}

String Operations​

replace​

Substring replacement:

replace
{{ 'Hello %name%'|replace({'%name%': user.name}) }}

split​

Splits a string into an array:

split
{% set parts = 'one,two,three'|split(',') %}
{# ['one', 'two', 'three'] #}

{{ 'one,two,three'|split(',', 2) }}
{# ['one', 'two,three'] #}

format​

String formatting, similar to sprintf:

format
{{ 'Hello, %s! You are %d years old.'|format(name, age) }}