String filters

Text-manipulation filters. Most are standard Shopify Liquid; those marked extension are Jekyll/LiquidJS-compatible additions. Every filter coerces its input to a string first.

Case

downcase

Lowercases the string.

{{ "Hello, World" | downcase }}   <!-- hello, world -->

upcase

Uppercases the string.

{{ "Hello, World" | upcase }}   <!-- HELLO, WORLD -->

capitalize

Uppercases the first character and lowercases the rest.

{{ "hello WORLD" | capitalize }}   <!-- Hello world -->

Trimming and whitespace

strip

Trims whitespace from both ends.

{{ "  hi  " | strip }}   <!-- hi -->

lstrip

Trims whitespace from the start.

{{ "  hi  " | lstrip }}   <!-- "hi  " -->

rstrip

Trims whitespace from the end.

{{ "  hi  " | rstrip }}   <!-- "  hi" -->

strip_newlines

Removes every \n and \r.

{{ "a\nb\nc" | strip_newlines }}   <!-- abc -->

newline_to_br

Inserts an HTML <br /> before each newline.

{{ "a\nb" | newline_to_br }}   <!-- a<br />\nb -->

normalize_whitespace

(extension) Collapses every run of whitespace to a single space and trims.

{{ "a   b\n c" | normalize_whitespace }}   <!-- a b c -->

Escaping

escape

HTML-escapes < > & " '. The result is marked safe, so it is not escaped again.

{{ "<a href='x'>" | escape }}   <!-- &lt;a href=&#39;x&#39;&gt; -->

escape_once

Like escape, but leaves existing entities intact (does not double-escape).

{{ "1 &lt; 2 &amp; 3" | escape_once }}   <!-- 1 &lt; 2 &amp; 3 -->

url_encode

Form-URL-encodes the string (space → +).

{{ "a b&c" | url_encode }}   <!-- a+b%26c -->

url_decode

Reverses url_encode.

{{ "a+b%26c" | url_decode }}   <!-- a b&c -->

cgi_escape

(extension) Produces an application/x-www-form-urlencoded value (space → +). Jekyll/LiquidJS-compatible.

{{ "a b & c" | cgi_escape }}   <!-- a+b+%26+c -->

uri_escape

(extension) Escapes for use in a URI while preserving reserved characters (;/?:@&=+$,); space → %20. Jekyll/LiquidJS-compatible.

{{ "http://x/a b?q=1&r=2" | uri_escape }}   <!-- http://x/a%20b?q=1&r=2 -->

xml_escape

(extension) Escapes & < > " ' to their XML/HTML entities. Jekyll/LiquidJS-compatible.

{{ "<a>'x'</a>" | xml_escape }}   <!-- &lt;a&gt;&#39;x&#39;&lt;/a&gt; -->

strip_html

Removes HTML tags (and the contents of <script>/<style>).

{{ "<b>hi</b><script>x()</script>" | strip_html }}   <!-- hi -->

Replacing and removing

replace

Replaces every occurrence of a substring.

{{ "a-b-c" | replace: "-", "+" }}   <!-- a+b+c -->

replace_first

Replaces the first occurrence.

{{ "a-b-c" | replace_first: "-", "+" }}   <!-- a+b-c -->

replace_last

(extension) Replaces the last occurrence. LiquidJS-compatible.

{{ "a-b-c" | replace_last: "-", "+" }}   <!-- a-b+c -->

remove

Removes every occurrence of a substring.

{{ "a-b-c" | remove: "-" }}   <!-- abc -->

remove_first

Removes the first occurrence.

{{ "a-b-c" | remove_first: "-" }}   <!-- ab-c -->

remove_last

(extension) Removes the last occurrence. LiquidJS-compatible.

{{ "a-b-c" | remove_last: "-" }}   <!-- a-bc -->

Adding and joining

append

Appends a string to the end.

{{ "/page" | append: ".html" }}   <!-- /page.html -->

prepend

Prepends a string to the start.

{{ "world" | prepend: "hello " }}   <!-- hello world -->

array_to_sentence_string

(extension) Joins an array into a sentence with an Oxford comma. The optional argument overrides the "and" connector. Jekyll/LiquidJS-compatible.

{{ tags | array_to_sentence_string }}         <!-- a, b, and c -->
{{ tags | array_to_sentence_string: "or" }}   <!-- a, b, or c -->

Slicing and splitting

slice

Extracts a substring: an offset (negative counts from the end) and an optional length (default 1). Also works on arrays.

{{ "hello" | slice: 1, 3 }}   <!-- ell -->
{{ "hello" | slice: -1 }}      <!-- o -->

split

Splits a string into an array on a separator (an empty separator splits per character).

{{ "a,b,c" | split: "," | join: " · " }}   <!-- a · b · c -->

truncate

Truncates to N characters (including the suffix, default ...).

{{ "The quick brown fox" | truncate: 9 }}   <!-- The qu... -->

truncatewords

Keeps the first N words, then the suffix (default ...).

{{ "The quick brown fox" | truncatewords: 2 }}   <!-- The quick... -->

Inspection

size

The length of a string (characters), array (elements), or object (keys); otherwise 0.

{{ "hello" | size }}   <!-- 5 -->

number_of_words

(extension) Counts whitespace-separated words (returns an integer). Jekyll/LiquidJS-compatible.

{{ "the quick brown fox" | number_of_words }}   <!-- 4 -->

default

Returns the argument when the input is blank, empty, or false.

{{ user.nickname | default: "Anonymous" }}

slugify

(extension) Lowercases, collapses each run of non-alphanumeric characters to a single -, and trims leading/trailing -. Jekyll/LiquidJS-compatible.

{{ "Hello, World!" | slugify }}   <!-- hello-world -->

See also