Mechanic include all filters available in Liquid's standard library. We also add a few of our own. :)

add_tag
add_tags
remove_tag
remove_tags

Use these filters to manipulate tag strings. All four of these tag filters are case-sensitive.

For background: Instead of working with tags as arrays, the Shopify API works with tags as comma-delimited strings; this makes it complicated to add or remove tags using only standard Liquid. These filters make it very simple.

{{ "a, b" | add_tag: "c" }}
=> a, b, c

{{ "a, b, e" | add_tags: "c", "d" }}
=> a, b, c, d, e

{{ "a, b, e" | add_tags: "c", "d", sort: false }}
=> a, b, e, c, d

{{ "a, b, c" | remove_tag: "b" }}
=> a, b

{{ "a, b, c" | remove_tags: "a", "c" }}
=> b

If supplied an array, these filters will return an array as well:

{{ "a,b,e" | split: "," | add_tags: "c", "d" | join: "-" }}
=> a-b-c-d-e

{{ "a,b,c,d,e" | split: "," | remove_tags: "c", "d" | join: "-" }}
=> a-b-e

base64
decode_base64

Use these filters to convert strings to their base64 representation, and back again.

{{ "hello!" | base64 }}
=> aGVsbG8h

{{ "aGVsbG8h" | decode_base64 }}
=> hello!

{{ "hello!" | base64 | decode_base64 }}
=> hello!

browser

This filter converts a browser user agent string into an object that represents the browser itself.

{% assign browser = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) GSA/79.0.259819395 Mobile/16G77 Safari/604.1" | browser %}

{{ browser }}

name: {{ browser.name }}

version: {{ browser.version }}
major version: {{ browser.version.major }}
minor version: {{ browser.version.minor }}

os: {{ browser.os }}
os name: {{ browser.os.name }}
os version: {{ browser.os.version }}
os major version: {{ browser.os.version.major }}
os minor version: {{ browser.os.version.minor }}

device: {{ browser.device }}
device name: {{ browser.device.name }}
device brand: {{ browser.device.brand }}
device model: {{ browser.device.model }}
Google 79.0.259819395

name: Google

version: 79.0.259819395
major version: 79
minor version: 0

os: iOS 12.4
os name: iOS
os version: 12.4
os major version: 12
os minor version: 4

device: iPhone
device name: iPhone
device brand: Apple
device model: iPhone

For reference, we use data from Browserscope to match user agents.

csv

Use this filter to convert a two-dimensional array to a CSV-formatted string. Useful when composing email attachments.

{% assign rows = array %}

{% assign header = array %}
{% assign header[0] = "Name" %}
{% assign header[1] = "Email" %}

{% assign rows[0] = header %}

{% for customer in shop.customers %}
  {% assign row = array %}
  {% assign row[0] = customer.first_name | append: " " | append: customer.last_name %}
  {% assign row[1] = customer.email %}
  {% assign rows[rows.size] = row %}
{% endfor %}

{{ rows | csv }}
=> "Name,Email\nJane Lopez,jane@example.com"

e164

This filter accepts a phone number – country code is required! – and outputs it in standard E.164 format. If the number does not appear valid, the filter returns nil.

{{ "1 (312) 456-7890" | e164 }}
=> "13124567890"

{{ "+43 670 1234567890" | e164 }}
=> "436701234567890"

{{ "000" | e164 | json }}
=> "null"

md5
sha256
hmac_sha1
hmac_sha256

These filters work identically to Shopify's implementation.

Read more: Shopify / Liquid reference / String filters

shopify

This filter accepts a GraphQL query string, sends it to Shopify, and returns the full response – including "data" and "errors".

Tip: Use Shopify's GraphiQL query builder to quickly and precisely assemble your queries.

{% capture query %}
query {
  shop {
    primaryDomain {
      host
      id
      sslEnabled
      url
    }
    myshopifyDomain
  }
}
{% endcapture %}

{{ query | shopify | json }}

=> {"data":{"shop":{"primaryDomain":{"host":"example.myshopify.com","id":"gid://shopify/Domain/1234567890","sslEnabled":true,"url":"https://example.myshopify.com"},"myshopifyDomain":"example.myshopify.com"}},"extensions":{"cost":{"requestedQueryCost":2,"actualQueryCost":2,"throttleStatus":{"maximumAvailable":1000.0,"currentlyAvailable":998,"restoreRate":50.0}}}}

parse_json

Use this filter to transform a JSON string into an object that can be inspected and traversed.

{% capture json_string %}
  {
    "hello": "world"
  }
{% endcapture %}

{% assign json_object = json_string | parse_json %}

hello {{ json_object.hello }}
hello world

sum

Use this on an array of numbers to quickly generate their sum.

{{ "[1, 2, 3]" | parse_json | sum }}
=> 6

unindent

Use this filter on strings to remove indentation from strings.

{% capture message %}
  Hello, friend!
  It's a mighty fine day!
{% endcapture %}

{{ message | unindent }}
Hello, friend!
It's a mighty fine day!
Did this answer your question?