append filter
Add text to the end of the input. If the input is not already a string it will be converted to one using the default behavior for its object type.
Liquid filters for string manipulation (format, split, replace, etc.).
Add text to the end of the input. If the input is not already a string it will be converted to one using the default behavior for its object type.
{{"fire" | append:"truck"}}firetruck
Capitalize words in a string
{{"a freight train running through the" | capitalize}}A Freight Train Running Through The
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) to form a valid CSS classname.
{{"a long classname!" | classname-}}<br />
{{-"a long classname!" | classname: "_"-}}<br />
{{-"a long classname!" | classname: ""}}a-long-classname<br /> a_long_classname<br /> alongclassname
Convert a string to lowercase
{{"A Freight TRAIN Running TRHOUGH THE" | downcase}}a freight train running through the
Encode a string to be output as HTML. All special HTML characters will be converted to their equivalent HTML character entities (eg: < becomes &lt;)
escape
<p title="{{"<p>A string with HTML & other characters</p>" | escape}}">...<p title="<p>A string with HTML & other characters</p>">...
Escapes all HTML and special characters.
escape_once
<p title="{{"<p>A string with some characters encoded & others not encoded</p>" | escape_once}}">...<p title="<p>A string with some characters encoded & others not encoded</p>">...
Escapes only characters that are not already escaped, avoiding double-encoding.
Encode a string to be output as HTML, without changing existing escaped entities.
escape
<p title="{{"<p>A string with HTML & other characters</p>" | escape}}">...<p title="<p>A string with HTML & other characters</p>">...
Escapes all HTML and special characters.
escape_once
<p title="{{"<p>A string with some characters encoded & others not encoded</p>" | escape_once}}">...<p title="<p>A string with some characters encoded & others not encoded</p>">...
Escapes only characters that are not already escaped, avoiding double-encoding.
This filter has been deprecated. You should use the json_encode filter 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.
{"something": {{'with "value"' | for_json}} }
{"something": {{null | for_json}} }{"something": "with \"value\""}
{"something": }The for_json filter works for input with valid values
{"something": {{'with "value"' | for_json}} }{"something": "with \"value\""}The for_json filter does not produce output if the input is null
{"something": {{null | for_json}} }{"something": }You can get away with the for_json filter by manually handling null/invalid values
{"something": {% if variable is_valid %}{{variable | for_json}}{% else %}null{% endif %} }The entire for_json filter is deprecated because the json_encode filter handles this and other uses cases more robustly
{"something": {{variable | json_encode }} }{
"null": {{ null | for_json }},
"date": {{ request.date | json_encode }},
"true": {{ true | json_encode }},
"3": {{ 3 | json_encode }},
"-4.2": {{ -4.2 | json_encode }},
"string": {{ 'Liquid is "cool"' | json_encode -}}
}{
"null": null,
"date": "2009-06-15T13:45:30.0000000Z",
"true": true,
"3": 3,
"-4.2": -4.2,
"string": "Liquid is \"cool\""
}Alias for escape, which encodes a string to be output as HTML. Although "h" is shorter, "escape" is preferred due to its improved readability and maintainability.
Decodes any encoded HTML entities (eg: &lt; becomes <)
{{"<p>A string with HTML & other characters</p>" | html_decode}}<p>A string with HTML & other characters</p>
Encode a string to be output as HTML. All special HTML characters will be converted to their equivalent HTML character entities (eg: < becomes <). This is functionally identical to the "escape" filter, though it may be more intuitive in some contexts - such as when using both the html_encode and html_decode filters to execute more advanced string manipulation.
<p title="{{"<p>A string with HTML & other characters</p>" | html_encode}}">...<p title="<p>A string with HTML & other characters</p>">...
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.
{% var input = 'ABC About' %}index: first occurrence
{{input | index: 'A'}}0
index: start after position
{{input | index: 'A', 1}}4
index: substring
{{input | index: 'Ab'}}4
index: with case-insensitive flag
{{input | index: 'Ab', 0, true}}0
index: not found
{{input | index: 'D'}}-1
last_index: last occurrence
{{input | last_index: 'A'}}4
last_index: search backward from position
{{input | last_index: 'A', 3}}0
last_index: substring
{{input | last_index: 'AB'}}0
last_index: with case-insensitive flag
{{input | last_index: 'AB', -1, true}}4
last_index: not found
{{input | last_index: 'a'}}-1
Decode a json encoded string
{{'\"liquid\" filter' | json_decode}}"liquid" filter
Encode the input object to be used as a JSON property.
Null values output the string "null". Dates are output using the ISO 8601 standard. Booleans are output as "true" or "false". Numbers are output as numbers. Strings are output as json encoded strings with quote marks properly escaped.
{"something": {{'with "value"' | for_json}} }
{"something": {{null | for_json}} }{"something": "with \"value\""}
{"something": }The for_json filter works for input with valid values
{"something": {{'with "value"' | for_json}} }{"something": "with \"value\""}The for_json filter does not produce output if the input is null
{"something": {{null | for_json}} }{"something": }You can get away with the for_json filter by manually handling null/invalid values
{"something": {% if variable is_valid %}{{variable | for_json}}{% else %}null{% endif %} }The entire for_json filter is deprecated because the json_encode filter handles this and other uses cases more robustly
{"something": {{variable | json_encode }} }{
"null": {{ null | for_json }},
"date": {{ request.date | json_encode }},
"true": {{ true | json_encode }},
"3": {{ 3 | json_encode }},
"-4.2": {{ -4.2 | json_encode }},
"string": {{ 'Liquid is "cool"' | json_encode -}}
}{
"null": null,
"date": "2009-06-15T13:45:30.0000000Z",
"true": true,
"3": 3,
"-4.2": -4.2,
"string": "Liquid is \"cool\""
}Returns the last 0-based location of the last occurrence of 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.
{% var input = 'ABC About' %}index: first occurrence
{{input | index: 'A'}}0
index: start after position
{{input | index: 'A', 1}}4
index: substring
{{input | index: 'Ab'}}4
index: with case-insensitive flag
{{input | index: 'Ab', 0, true}}0
index: not found
{{input | index: 'D'}}-1
last_index: last occurrence
{{input | last_index: 'A'}}4
last_index: search backward from position
{{input | last_index: 'A', 3}}0
last_index: substring
{{input | last_index: 'AB'}}0
last_index: with case-insensitive flag
{{input | last_index: 'AB', -1, true}}4
last_index: not found
{{input | last_index: 'a'}}-1
Removes whitespace from the beginning of a string
{% var sentence_with_extra_whitespace = ' The quick brown fox jumps over the lazy dog. ' %}[{{sentence_with_extra_whitespace | lstrip}}][The quick brown fox jumps over the lazy dog. ]
[{{sentence_with_extra_whitespace | rstrip}}][ The quick brown fox jumps over the lazy dog.]
[{{sentence_with_extra_whitespace | strip}}][The quick brown fox jumps over the lazy dog.]
Add <br /> tags in front of all newlines in the current string
{%- capture input -%}
ABC
DEF
GHI
{%- endcapture -%}
{{-input | newline_to_br}}ABC<br /> DEF<br /> GHI
Adds the operand to the current value. Note that this filter behaves differently if the current value is a string
The plus filter may behave differently when used with string input. When used with a string input it may append text to the current value, although that behavior is deprecated and should be replaced by the append filter for optimal forward-compatibility.
Add text to the beginning of the input. If the input is not already a string it will be converted to one using the default behavior for its object type.
{{"truck" | prepend:"fire"}}firetruck
Remove all occurrences of search from the current string.
{% var input = 'apply for apples app development' %}{{input | remove:'app'}}ly for les development
{{input | remove_first:'app'}}ly for apples app development
{{input | remove_first:'app', 2}}ly for les app development
The remove and remove_first filters are case-sensitive
{{ input | remove:'App' }}apply for apples app development
Remove the first occurrence(s) of search from the current string.
{% var input = 'apply for apples app development' %}{{input | remove:'app'}}ly for les development
{{input | remove_first:'app'}}ly for apples app development
{{input | remove_first:'app', 2}}ly for les app development
The remove and remove_first filters are case-sensitive
{{ input | remove:'App' }}apply for apples app development
Replace all occurrences of search inside the current string with replacement
{% var input = 'apply for apples app development' %}{{input | replace:'app', 'mop'}}moply for moples mop development
{{input | replace_first:'app', 'mop'}}moply for apples app development
{{input | replace_first:'app', 'mop', 2}}moply for moples app development
The replace and replace_first filters are case-sensitive
{{ input | replace:'App', 'Mop'}}apply for apples app development
Replace the first occurrence(s) of search inside the current string with replacement
{% var input = 'apply for apples app development' %}{{input | replace:'app', 'mop'}}moply for moples mop development
{{input | replace_first:'app', 'mop'}}moply for apples app development
{{input | replace_first:'app', 'mop', 2}}moply for moples app development
The replace and replace_first filters are case-sensitive
{{ input | replace:'App', 'Mop'}}apply for apples app development
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 captured groups.
{% var input = 'The quick brown dog jumps over the lazy dog.' %}{{input | replace_regex:'/dog/i', 'dragon'}}The quick brown dragon jumps over the lazy dragon.
{{input | replace_regex_first:'/dog/i', 'dragon'}}The quick brown dragon jumps over the lazy dog.
{{input | replace_regex_first:'(quick|brown|lazy)', 'adjective:$1', 2}}The adjective:quick adjective:brown dog jumps over the lazy dog.
Replace the first occurrence(s) 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 captured groups.
{% var input = 'The quick brown dog jumps over the lazy dog.' %}{{input | replace_regex:'/dog/i', 'dragon'}}The quick brown dragon jumps over the lazy dragon.
{{input | replace_regex_first:'/dog/i', 'dragon'}}The quick brown dragon jumps over the lazy dog.
{{input | replace_regex_first:'(quick|brown|lazy)', 'adjective:$1', 2}}The adjective:quick adjective:brown dog jumps over the lazy dog.
Reverses the input string or list.
When used with a string as input, the result is a string. Otherwise the result is a list.
When used on null input
{{null | reverse | object_type}}null
When used on a string
{{"string" | reverse}}gnirts
When used on a list
{{"item1,item2,item3" | split:"," | reverse | join:" "}}item3 item2 item1
Removes whitespace from the end of a string
{% var sentence_with_extra_whitespace = ' The quick brown fox jumps over the lazy dog. ' %}[{{sentence_with_extra_whitespace | lstrip}}][The quick brown fox jumps over the lazy dog. ]
[{{sentence_with_extra_whitespace | rstrip}}][ The quick brown fox jumps over the lazy dog.]
[{{sentence_with_extra_whitespace | strip}}][The quick brown fox jumps over the lazy dog.]
Returns the length of the input string or list.
If the input is not a string or list returns 0.
When used on null input
{{null | size}}0
When used on a string
{{"string" | size}}6
When used on a list
{{"item1,item2,item3" | split:"," | size}}3
When used on anything else
{{request.date | size}}0
Return a part of the current string or list.
If the input is a string this will return a string. If it is a list it will return a list. Otherwise it will not do anything and will return the unaltered input.
When used on null input
{{null | slice | object_type}}null
When used on a string
{{"string" | slice: 2}}ring
{{"string" | slice: 2, 3}}rin
When used on a list
{{"ab,cd,ef,gh,ij,kl,mn,op,qr,st,uv,wx,yz" | split:"," | slice: 2, 3 | join: " "}}ef gh ij
With a negative start value
{{"string" | slice: -2}}ng
With a negative length
{{"string" | slice: 2, -1}}rin
With a negative start and length
{{"string" | slice: -4, -2}}ri
With length = 0
{{"string" | slice: -4, 0}}ring
With a really large negative start
{{"string" | slice: -20}}string
With start higher than the input length
[{{"string" | slice: 20}}][]
With a calculated length less or equal to 0
[{{"string" | slice: 2, -4}}][]
Does not do anything if the object is not a string or a list
{{ request.date | slice: 2 }}9/9/2009 12:00:00 AM
Split a string into a list of substrings separated by the given separator
{{'The quick brown fox jumps over the lazy dog.' | split:" " | json_encode}}["The","quick","brown","fox","jumps","over","the","lazy","dog."]
Split on multiple characters
{{'She sells sea shells on the sea shore.' | split:"sea" | json_encode}}["She sells "," shells on the "," shore."]
Does not include empty strings in the result
{{"110100101" | split:'1' | json_encode}}["0","00","0" | split:'1'}}
Use an empty pattern to split the string into individual characters
{{"abcdef" | split:'' | json_encode}}["a","b","c","d","e","f"]
Removes whitespace from the beginning and end of a string
{% var sentence_with_extra_whitespace = ' The quick brown fox jumps over the lazy dog. ' %}[{{sentence_with_extra_whitespace | lstrip}}][The quick brown fox jumps over the lazy dog. ]
[{{sentence_with_extra_whitespace | rstrip}}][ The quick brown fox jumps over the lazy dog.]
[{{sentence_with_extra_whitespace | strip}}][The quick brown fox jumps over the lazy dog.]
Removes all HTML tags from a string and can optionally unescape HTML entities.
Pass true for unescapeHtmlEntities unless there is a reason to preserve encoded entities. When the input may be either HTML or already plain text, pass true for both arguments so onlyUnescapeIfHtml prevents entity decoding unless the input looks like HTML. Avoid mixing plain text and HTML markup in one value: a mixed value that does not begin like HTML will have its tags removed but keep its entities encoded. This filter uses simple pattern matching to recognize and remove HTML. If the input is poorly formatted or contains unusual character sequences - particularly involving the '<' and '>' characters - this could result in unexpected behavior.
Default behavior preserves HTML entities
{% var input = "<p>This is > that</p>" %}
{{- input | strip_html -}}This is > that
Both arguments default to false, preserving the filter's previous behavior: HTML tags are removed and HTML entities remain encoded.
Preferred for known HTML
{% var input = "<p>This is > that</p>" %}
{{- input | strip_html: true -}}This is > that
Pass true for unescapeHtmlEntities when the input is known to be HTML. This is the recommended form unless the encoded entities must be preserved.
Conditionally unescape known HTML
{{- "<p>This is HTML & characters should be unescaped</p>" | strip_html: true, true -}}This is HTML & characters should be unescaped
When the input type is uncertain, pass true for both arguments. Because this input looks like HTML, its entities are unescaped after its tags are removed.
Preserve entities in plain text
{{- "This is plain text & characters should be unchanged" | strip_html: true, true -}}This is plain text & characters should be unchanged
Because this input does not look like HTML, onlyUnescapeIfHtml leaves its entities encoded. HTML tags would still be removed if any were present.
Avoid mixed HTML and plain text
{{- "This is <strong>MIXED</strong> & characters <em>may</em> be changed" | strip_html: true, true -}}This is MIXED & characters may be changed
Avoid mixed HTML and plain-text input whenever possible. Because this value begins as plain text, its tags are removed but its entities remain encoded.
{%- capture input -%}
<script>... this will strip script blocks ...</script>
<style>... this will also strip style blocks ...</style>
<!-- and this will strip HTML comments -->
<p title="this will strip the open and close tags but leave the content intact">The quick brown fox jumps over the lazy dog.</p>
<br />
<img src="..." alt="note that this will also strip images" />
{%- endcapture -%}
{{-input | strip_html | strip}}The quick brown fox jumps over the lazy dog.
Could result in unexpected behavior with poorly formatted input
{%- capture input -%}
//script outside of a script tag
if (a < b)
doSomething();
if(a > b)
doSomethingElse();
//endscript
{%- endcapture -%}
{{-input | strip_html | strip}}//script outside of a script tag if (a b) doSomethingElse(); //endscript
Could result in unexpected behavior with poorly formatted input
{%- capture input -%}
<p>The following invalid markup contains an unescaped > character in an HTML attribute:
<input type="button" onclick="if(a>b) doSomething();" value="Do Something" /></p>
{%- endcapture -%}
{{-input | strip_html | strip}}The following invalid markup contains an unescaped > character in an HTML attribute: b) doSomething();" value="Do Something" />
Removes all newlines from a string
{%- capture input -%}
ABC
DEF
GHI
{%- endcapture -%}
{{-input | strip_newlines}}ABCDEFGHI
Multiply the input by the operand.
This filter currently behaves differently if the input is a string and the operand is an integer. In that case the result is a list of strings with input repeated operand times.
Current functionality
{{"string" | times:2 | json_encode}}["string","string"]
Future functionality
{{"string" | times:2 | json_encode}}Liquid error
Potential Replacement
{%- map string for i in (1..2) %}string{% endmap -%}
{{-string | json_encode}}["string","string"]
Alternate Replacement
{%- capture string %}{% for i in (1..2) %}string{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture -%}
{{-string | split:',' | json_encode}}["string","string"]
Truncates a string down to length characters. If the original string is longer than length characters, the result will end with truncate_string.
{% var input = 'The quick brown fox jumps over the lazy dog.' %}truncate
{{input | truncate:33}}The quick brown fox jumps over...
{{input | truncate:33 | size}}33
{{input | truncate:31, " (more)"}}The quick brown fox jump (more)
{{input | truncate:33, ""}}The quick brown fox jumps over th
{{'Shorter than 33 characters' | truncate: 33}}Shorter than 33 characters
truncate_to_word
{{input | truncate_to_word:30}}The quick brown fox jumps...
{{input | truncate_to_word:30 | size}}28
{{input | truncate_to_word:30, false}}The quick brown fox jumps over...
{{input | truncate_to_word:30, false | size}}33
truncate_words
{{input | truncate_words:3}}The quick brown...
{{input | truncate_words:3, " (more)"}}The quick brown (more)
{{input | truncate_words:9}}The quick brown fox jumps over the lazy dog.
Truncates a string down to length characters. If the string would be broken in the middle of a word, ensures that the break happens either before or after the word. If the string is truncated it will end with truncate_string.
The truncate_to_word uses a naive algorithm for word counting that considers words as one or more letters, digits, underscores, or apostrophes. All other characters are considered non-word characters in between words. This means that a string could still be truncated in the middle of a hypenated word or a word with other non-word characters such as "awe-inspiring", "r&r", "1.25", "3/4", etc.... This filter also does not strip or consolidate whitespace, or handle HTML markup any different than normal text.
{% var input = 'The quick brown fox jumps over the lazy dog.' %}truncate
{{input | truncate:33}}The quick brown fox jumps over...
{{input | truncate:33 | size}}33
{{input | truncate:31, " (more)"}}The quick brown fox jump (more)
{{input | truncate:33, ""}}The quick brown fox jumps over th
{{'Shorter than 33 characters' | truncate: 33}}Shorter than 33 characters
truncate_to_word
{{input | truncate_to_word:30}}The quick brown fox jumps...
{{input | truncate_to_word:30 | size}}28
{{input | truncate_to_word:30, false}}The quick brown fox jumps over...
{{input | truncate_to_word:30, false | size}}33
truncate_words
{{input | truncate_words:3}}The quick brown...
{{input | truncate_words:3, " (more)"}}The quick brown (more)
{{input | truncate_words:9}}The quick brown fox jumps over the lazy dog.
Truncates the input string down to length words. If the input is longer than length words, appends truncate_string to the end of the truncated string.
The truncate_words uses a naive algorithm for word counting that considers words as one or more letters, digits, underscores, or apostrophes. All other characters are considered non-word characters in between words. This means that hyphenated words such as "awe-inspiring" are counted as two words, as are "words" with other characters between letters, such as "r&r", "1.25", "3/4", etc.... This filter also does not strip or consolidate whitespace, or handle HTML markup any different than normal text.
{% var input = 'The quick brown fox jumps over the lazy dog.' %}truncate
{{input | truncate:33}}The quick brown fox jumps over...
{{input | truncate:33 | size}}33
{{input | truncate:31, " (more)"}}The quick brown fox jump (more)
{{input | truncate:33, ""}}The quick brown fox jumps over th
{{'Shorter than 33 characters' | truncate: 33}}Shorter than 33 characters
truncate_to_word
{{input | truncate_to_word:30}}The quick brown fox jumps...
{{input | truncate_to_word:30 | size}}28
{{input | truncate_to_word:30, false}}The quick brown fox jumps over...
{{input | truncate_to_word:30, false | size}}33
truncate_words
{{input | truncate_words:3}}The quick brown...
{{input | truncate_words:3, " (more)"}}The quick brown (more)
{{input | truncate_words:9}}The quick brown fox jumps over the lazy dog.
Convert a string to uppercase
{{"A Freight TRAIN Running TRHOUGH THE" | upcase}}A FREIGHT TRAIN RUNNING THROUGH THE
Decode a url encoded string.
url_encode
{{"liquid filter" | url_encode}}liquid%20filter
{{"liquid filter" | url_encode:true}}liquid+filter
url_decode
{{"liquid%20filter" | url_decode}}liquid filter
{{"liquid+filter" | url_decode}}liquid+filter
{{"liquid+filter" | url_decode:true}}liquid filter
Encode a string to be used in a URL.
url_encode
{{"liquid filter" | url_encode}}liquid%20filter
{{"liquid filter" | url_encode:true}}liquid+filter
url_decode
{{"liquid%20filter" | url_decode}}liquid filter
{{"liquid+filter" | url_decode}}liquid+filter
{{"liquid+filter" | url_decode:true}}liquid filter