Reference: Liquid Filters and Operators

The Liquid templating language offers a number of standard filters and comparison operators for customizing the output of Liquid templates. In addition to the standard filters and operators, Duoplane has added several custom ones to make working with Liquid more powerful and convenient.

Filters


Strings

fixed_width

Creates a fixed-width version of the string. Arguments:

  1. The width of the output
  2. The padding character (default = " ")
  3. Whether the original string should be aligned on the left or right
Input: {{ "1234" | fixed_width: 10, "0", "right" }}
Output: 0000001234

replace_regex

Replaces every occurrence of the first argument in a string with the second argument. Unlike the standard filter replace, this filter uses a regular expression match against the first argument. Arguments:

  1. The regular expression matcher
  2. The replacement text
  3. Optional: Whether the match is case-sensitive (Default = false)
Input: {{ "ABC12345EFG" | replace_regex: "[^\d]", "" }}
Output: 12345

replace_newlines

Replaces every newline / line break / carriage return character with the second argument. Arguments:

  1. The replacement text (Default = " ")

For example, if "description" is a string with a line break, such as:

Hello,

Nice to meet you
Input: {{ description | replace_newlines: " " }}
Output: Hello, Nice to meet you

xml_escape

Escapes any characters in the string so that the string can be used in XML

Input: {{ 'how "are" <you>' | xml_escape }}
Output: how &quot;are&quot; &lt;you&gt;

Arrays

push

Appends the argument to the end of the array. If my_list is an array of: ["apples", "oranges"]

Input: {{ my_list | append: "bananas" | join: ", " }}
Output: apples, oranges, bananas

Numbers

with_precision

Rounds a number to a given precision.

Input: {{ 50.9634 | with_precision: 2 }}
Output: 50.96

to_currency

Formats a number as currency Arguments:

  1. The precision (default = 2)
  2. Currency symbol (default = "")
  3. Separator between the units (default = ".")
  4. Thousands delimiter (default = ",")
Input: {{ 123456.54321 | to_currency: 2, "$" }}
Output: $123,456.54

Dates and times

plus_hours

Adds a certain number of hours to a starting time If an order was created on Jan 1, 2020 at 12:00pm UTC:

Input: {{ order.created_at | plus_hours: 2 }}
Output: 2020-01-01 14:00:00 UTC

plus_business_hours

Adds a certain number of business hours to a starting time. Business time is defined as Monday through Friday, 9am to 5pm. Arguments:

  1. The number of business hours to add
  2. The time zone for determining business hours

If an order was created on March 7, 2020 at 9:00pm PST: (a Saturday)

Input: {{ order.created_at | plus_business_hours: 2, "Pacific Time (US & Canada)" }}
Output: 2020-03-09 11:00:00 -0700

in_time_zone

Converts a time to be outputted in a specific time zone. Arguments:

  1. The time zone

If an order was created at 1:00pm Pacific:

Input: {{ order.created_at | in_time_zone: "Eastern Time (US & Canada)" | date: "%H:%M" }}
Output: 16:00

parse_time

Uses natural language parsing to convert a string representation of a date or time to a Time object that can be used by Liquid. Arguments:

  1. A date or time string

If today is Monday:

Input: {{ "yesterday" | parse_time | date: "%a" }}
Output: Sun

Other

json_parse

Converts a JSON string into a Liquid object that can be used elsewhere in the template

Input: {% assign mapping = '{ "color": "blue", "size": 5 }' | json_parse %}{{ mapping["color"] }}
Output: blue

to_barcode

Outputs a barcode representation of a string as an SVG image tag Arguments:

  1. The barcode height in pixels (default = 50)
  2. The barcode thickness in pixels (default = 1)
Input: {{ "0123456789" | to_barcode }}
Output: blobid1.dat

 

Operators

matches

Checks for whether the left side matches the right side after the right side is converted to a case-insensitive regular expression.

Input: {% if "UPS Ground" matches "^UPS.*$" %}It is UPS.{% endif %}
Output: It is UPS.

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Comments

0 comments

Article is closed for comments.