String Filters

String filters are used to modify the output of strings.

  1. append
  2. capitalize
  3. downcase
  4. escape
  5. newline_to_br
  6. prepend
  7. remove
  8. remove_first
  9. replace
  10. replace_first
  11. split
  12. strip_html
  13. strip_newlines
  14. truncate
  15. truncatewords
  16. upcase

append

Appends characters to a string.

Input
  {{ 'sales' | append: '.jpg' }}
Output
  sales.jpg

capitalize

Capitalizes the first word in a string.

Input
  {{ 'capitalize me' | capitalize }}
Output
  Capitalize me

downcase

Converts a string into lowercase.

Input
  {{ 'UPPERCASE' | downcase }}
Output
  uppercase

escape

Escapes a string.

Input
  {{ "<p>test</p>" | escape }}
Output
  <!-- The <p> tags are not rendered -->
    <p>test</p>

newline_to_br

Inserts a <br> linebreak HTML tag in front of each line break in a string.

Input
  {% capture var %}
  One
  Two
  Three
  {% endcapture %}
  {{ var | newline_to_br }}
Output
  One <br>
  Two <br>
  Three <br>

prepend

Prepends characters to a string.

Input
  {{ 'sale' | prepend: 'Made a great ' }}
Output
  Made a great sale

remove

Removes all occurrences of a substring from a string.

Input
  {{ "Hello, world. Goodbye, world." | remove: "world" }}
Output
  Hello, . Goodbye, .

remove_first

Removes only the first occurrence of a substring from a string.

Input
  {{ "Hello, world. Goodbye, world." | remove_first: "world" }}
Output
  Hello, . Goodbye, world.

replace

Replaces all occurrences of a string with a substring.

Input
  <!-- amenity.title = "Awesome Swimming Pool" -->
    {{ amenity.title | replace: 'Awesome', 'Huge' }}
Output
  Huge Swimming Pool

replace_first

Replaces the first occurrence of a string with a substring.

Input

  <!-- product.title = "Awesome Awesome Swimming Pool" -->
    {{ product.title | replace_first: 'Awesome', 'Huge' }}
Output
  Huge Awesome Swimming Pool

split

The split filter takes on a substring as a parameter. The substring is used as a delimiter to divide a string into an array. You can output different parts of an array using array filters.

Input
  {% assign words = "Hi, how are you today?" | split: ' ' %}

  {% for word in words %}
  {{ word }}
  {% endfor %}
Output
  Hi,
  how
  are
  you
  today?

strip_html

Strips all HTML tags from a string.

Input
  {{ "<h1>Hello</h1> World" | strip_html }}
Output
  Hello World

strip_newlines

Removes any line breaks/newlines from a string.

  {{ rental.description | strip_newlines }}

truncate

Truncates a string down to ‘x’ characters, where x is the number passed as a parameter. An ellipsis (…) is appended to the string and is included in the character count.

Input
  {{ "The cat came back the very next day" | truncate: 10 }}
Output
  The cat...

truncatewords

Truncates a string down to ‘x’ words, where x is the number passed as a parameter. An ellipsis (…) is appended to the truncated string.

Input
  {{ "The cat came back the very next day" | truncatewords: 4 }}
Output
  The cat came back...

upcase

Converts a string into uppercase.

Input
  {{ 'i want this to be uppercase' | upcase }}
Output
  I WANT THIS TO BE UPPERCASE