String Filters

String Filters

Use string filters to manipulate strings.

append

Add text to the end of the current string

append: String text

{{"fire" | append:"truck"}}
firetruck

capitalize

Capitalize words in a string

capitalize

{{"a freight train running through the" | capitalize}}
A Freight Train Running Through The

classname

Removes all non-alphanumeric characters other than dashes and underscores from a string and replaces them with the separator (or nothing if the separator is empty).

classname: String separator = "-"

{{"a long classname!" | classname}}    a-long-classname
{{"a long classname!" | classname: "_"}}    a_long_classname
{{"a long classname!" | classname: ""}}    alongclassname

downcase

Convert a string to lowercase

downcase

{{"A Freight TRAIN Running TRHOUGH THE" | downcase}}
a freight train running through the

escape

Encode a string to be output as HTML. All special HTML characters will be converted to their equivalent HTML character entities (eg: < becomes <)

escape

{{"<p>A string with HTML & other characters</p>" | escape}}
&lt;p&gt;A string with HTML &amp; other characters&lt;/p&gt;

for_json

Deprecated! Use json_encode instead.

Encode a string to be used output as JSON. Unlike json_encode, if the string is null this will return an empty string instead.

for_json

{"something": {{null | for_json}} }    "{"something": }

h

Alias for escape

index

Returns the 0-based location of the find string in the current string, or -1 if it cannot be found. If start is greater than 0, the search will begin at the specified index. To ignore capitalization, set ignoreCase to true.

index: String find Int start = 0 True/False ignorecase = false

{% var input = 'ABC About' %}
{{input | index: 'A'}}    0
{{input | index: 'A', 1}}    4
{{input | index: 'Ab'}}    4
{{input | index: 'Ab', 0, true}}    0
{{input | index: 'D'}}    -1

json_decode

Decode a json encoded string.

json_decode

{{'\"liquid\" filter' | json_decode}}
"liquid" filter

json_encode

Encode a string to be used output as JSON.

json_encode

{"something": {{entity.title | json_encode}} }    {"something": "Escaped entity title" }
{"nothing": {{null | json_encode}} }    {"nothing": null }

last_index

Returns the last 0-based location of the find string in the current string, or -1 if it cannot be found. If start is greater than or equal to 0, the search will begin at the specified index. To ignore capitalization, set ignoreCase to true.

index: String find Int start = -1 True/False ignorecase = false

{% var input = 'ABC About' %}
{{input | last_index: 'A'}}    4
{{input | last_index: 'A', 3}}    0
{{input | last_index: 'AB'}}    0
{{input | last_index: 'AB', -1, true}}    4
{{input | last_index: 'a'}}    -1

newline_to_br

Add "<br />" tags in front of all newlines in the current string

plus

Appends the text to the current value. Note that this filter behaves differently if the current value is not a string.

plus: string text

{{'Some' | plus:'Thing'}}
SomeThing

plus: int text

{{4 | plus:2}}
6

prepend

Add text to the beginning of the current string

prepend: String text

{{'truck' | prepend:'fire'}}
firetruck

remove

Remove all occurrences of search from the current string

remove: String search

{{'apply for apples app development' | remove:'app'}}
ly for les development

remove_first

Remove the first occurrence of search from the current string

remove_first: String search

{{'apply for apples app development' | remove_first:'app'}}
ly for apples app development

replace

Replace all occurrences of search inside the current string with replacement.

replace: String search String replacement = ""

{{'The quick brown fox jumps over the lazy dog.' | replace:'dog', 'dragon'}}
The quick brown fox jumps over the lazy dragon.

replace_regex

Replace all occurrences of pattern inside the current string with replacement using a regular expression - making it possible to search for more complicated expressions and replace using the resulting matching groups. .

replace_regex: String pattern String replacement = ""

{{'The quick brown fox jumps over the lazy dog.' | replace_regex:'/dog/gi', 'dragon'}}
The quick brown fox jumps over the lazy dragon.

replace_first

Replace the first numReplacements occurrences of search inside the current string with replacement.

replace_regex: String search String replacement = "" int numReplacements = 1

{{'The quick brown dog jumps over the lazy dog.' | replace_regex:'dog', 'dragon'}}
The quick brown dragon jumps over the lazy dog.

replace_regex_first

Replace the first numReplacements occurrences of pattern inside the current string with replacement using a regular expression - similar to the replace_regex filter.

replace_regex_first: String pattern String replacement = "" int numReplacements = 1

{{'The quick brown dog jumps over the lazy dog.' | replace_regex_first:'/dog/gi', 'dragon'}}
The quick brown dragon jumps over the lazy dog.

reverse

Reverses the current string. Can also be used to reverse a list.

reverse

{{'123456789' | reverse}}
987654321

size

Return the size of the current string

size

{{'The quick brown fox jumps over the lazy dog.' | size}}
44

slice

Return a part of the current string. If start is negative, will return len characters from the end of the string. If len is 0, will return all remaining characters after start. If len is negative, will return up to len characters from the end of the string.

slice: int start int len = 0

{{'The quick brown fox jumps over the lazy dog.' | slice:31}}
the lazy dog.

split

Split a string into a list of substrings separated by the given pattern

split: String pattern

{{'The quick brown fox jumps over the lazy dog.' | split:" "}}
["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]

strip

Removes whitespace from the beginning and end of a string

split

{{' The quick brown fox jumps over the lazy dog. ' | strip}}
The quick brown fox jumps over the lazy dog.

strip_html

Removes all HTML tags from a string

split

{{'<p>The quick brown fox jumps over the lazy dog.</p>' | strip_html}}
The quick brown fox jumps over the lazy dog.

strip_newlines

Removes all newlines from a string

times

Repeats the current value count times. This filter behaves differently if the current value is a string or if count is not an integer.

times: int count

{{'Cool ' | times:6}}
Cool Cool Cool Cool Cool Cool

times: int count

{{4 | times:2}}
8

truncate

Truncates a string down to length characters. If the original string is longer than length characters, appends truncate_string to the end of the truncated string.

truncate: int length = 50 String truncate_string = "..."

{{'The quick brown fox jumps over the lazy dog.' | truncate:31}}
The quick brown fox jumps over...

truncate_words

Truncates a string down to length words. If the original string is longer than length words, appends truncate_string to the end of the truncated string.

truncate_words: int length = 50 String truncate_string = "..."

{{'The quick brown fox jumps over the lazy dog.' | truncate_words:6}}
The quick brown fox jumps over...

upcase

Convert a string to uppercase

upcase

{{"a freight train running through the" | upcase}}
A FREIGHT TRAIN RUNNING TRHOUGH THE

url_decode

Decode a UrlEncoded string. If formData is true, decodes the string as if it were submitted as part of an html form (eg: decodes the + character to a space character). If formData is false, decodes the string as a normal url_encoded string (eg: leaves the + character alone).

url_decode: boolean formData = false

{{"liquid%20filter" | url_decode}}
liquid filter

url_encode

Encode a string to be used in a URL. If formData is true, encodes the string as if it were submitted as part of an html form (eg: encodes the space character to the + character). If formData is false, encodes the string as a normal url string (eg: encodes the space character as %20).

url_encode: boolean formData = false

{{"liquid filter" | url_encode}}    liquid%20filter
{{"liquid filter" | url_encode: true}}    liquid+filter