Platform
Templates
Liquid helpers reference

Liquid helpers

A reference to help you work with the Liquid templating language in Knock.

The Knock template editor uses Liquid syntax for control flow and variable declaration. Here are a few of the most common Liquid keywords our customers use within Knock. We recommend referencing the Liquid documentation for comprehensive syntax and usage information.

KeywordDescription
{{ }}Denotes rendering output of an object or variable.
{% %}Denotes logic and control flow.
if/else/elsifConditional branching.
case/whenCreates a switch statement to execute a particular block of code when a variable has a specified value.
and/orAdd additional conditions to a tag.
forRepeatedly executes a block of code.
assignCreates a new named variable.
captureCaptures the string inside of the opening and closing tags and assigns it to a variable.
renderRenders a partial with the given key and attributes.

Knock-specific Liquid helpers

#
HelperDescriptionExampleOutput
timezone
Takes an ISO 8601 timestamp and returns it in the IANA tz database timezone provided. You can use the built-in timestamp template variable to reference the current datetime. When formatting with the date filter, we recommend using the timezoneoption on the date filter instead.
{{ timestamp | timezone: "America/New_York"}}2023-12-31T19:30:00-05:00
date
This filter has been extended to take an additional timezone option.
{{ startDate | date: "%-I:%M %p %Z" , "America/New_York" }}2:30 PM EST
format_date_in_locale
Converts an ISO 8601 timestamp to a localized date string. Requires alocale parameter and accepts an optional date_format: "short"(default), "medium", "long", or "full". See the date format options table below for examples.
{{ timestamp | format_date_in_locale: "en-GB", "long" }}31 December 2023
format_datetime_in_locale
Converts an ISO 8601 timestamp to a localized date and time string. Requires locale, date_format, and time_format parameters. Date and time formats can be: "short" (default), "medium", "long", or"full". See the date format options and time format options tables below for examples.
{{ timestamp | format_datetime_in_locale: "en-GB", "medium", "short" }}31 Dec 2023, 14:30
format_number
Takes an integer and formats it to the local number format of the provided locale.
{{ 10000 | format_number: "en" }}10,000
currency
Takes an integer and returns a USD formatted value with two decimal points. You can pass a currency type and a locale through to the currency helper to tell it which currency to use.
{{ 10 | currency: "GBP", "en" }}£10.00
rounded_currency
Takes an integer and returns a USD formatted value rounded to nearest whole number. You can pass a currency type and a locale through to the currency helper to tell it which currency to use.
{{ 10.99 | rounded_currency: "GBP", "en" }}£11
jsonTakes a value and returns as a formatted JSON string.{{ recipient | json }}{"id": "user_123", "name": "John Hammond"}
pluralizeTakes an integer and a pluralize helper with two strings. If the integer is one, the helper returns the first string. If the helper is greater than one, it returns the second string.{{ total_actors | pluralize: "user", "users" }}users
titlecaseTakes a string and reformats it into Title case.{{ project_name | titlecase }}My Project Name
md5Takes a string and returns an md5 hash.{{ recipient.id | md5 }}-
sha256Takes a string and returns an sha256 hash.{{ recipient.id | sha256 }}-
hmac_sha256
Takes a string and returns an hmac hash given a key provided to hmac_sha256 helper.
{{ recipient.id | hmac_sha256: "some-key" }}-
intersectReturns the intersection of two arrays (the elements common to both).{{ arr1 | intersect: arr2 }}["Acme Corp", "InGen"]
from_markdownTakes a string of markdown and converts it to HTML.{{ data.comment_body | from_markdown }}<h1>Hello</h1>
compare_versions
For a given semantic version string, compares it to a second version string provided as an argument. Returns an integer indicating the order of the versions; -1 if the first version is less than the second, 0 if they are equal, and 1 if the first version is greater than the second.
{{ "1.0.0" | compare_versions: "2.0.0" }}-1

Dynamic data filters

#

Knock provides a set of Liquid filters that dynamically load data from your Knock environment at template render time. These filters enable you to reference entities like users, objects, tenants, and subscriptions in your templates without passing all of the data in your workflow trigger call. For more details and examples, see referencing data in templates.

FilterDescriptionExample
userLoads a user by their identifier. Returns a serialized User with all custom properties, as well as id, name, email, phone_number, created_at, and updated_at.{% assign user = "chris" | user %}
objectLoads an object by its identifier and collection. Returns a serialized Object with all custom properties, as well as id, collection, created_at, and updated_at. Requires a collection parameter.{% assign project = "proj_1" | object: "projects" %}
tenantLoads a tenant by its identifier. Returns a serialized Tenant with all custom properties, as well as id, created_at, and updated_at.{% assign tenant = "acme" | tenant %}
subscriptionsLoads up to 25 active subscriptions for a user by their identifier. Returns a list of subscription objects, each containing the subscribed object and any custom properties set on the subscription.{% assign subs = recipient.id | subscriptions %}

Date format options

#

The format_date_in_locale and format_datetime_in_locale helpers accept date format options that control how the date portion is displayed:

ValueDescriptionExample (en-US)Example (en-GB)
shortAbbreviated date format.12/31/2331/12/2023
mediumStandard date format.Dec 31, 202331 Dec 2023
longFull date format.December 31, 202331 December 2023
fullComplete date format.Sunday, December 31, 2023Sunday, 31 December 2023

Time format options

#

The format_datetime_in_locale helper accepts time format options that control how the time portion is displayed:

ValueDescriptionExample (en-US)Example (en-GB)
shortAbbreviated time format.2:30 PM14:30
mediumStandard time format.2:30:45 PM14:30:45
longFull time format.2:30:45 PM EST14:30:45 GMT
fullComplete time format.2:30:45 PM Eastern Standard Time14:30:45 Greenwich Mean Time

Localization parameters

#

A few of Knock's Liquid helpers (such as format_date_in_locale, format_datetime_in_locale, format_number, currency, and rounded_currency) take an optional locale parameter to format the output of the helper into a localized format. You can find a list of supported locales below. If we're missing a locale that you'd like us to support, please reach out.

Supported locales: af, ar, az, be, bg, bn, bs, ca, cs, cy, da, de, de-DE, el, en, en-US, en-GB, eo, es, es-419, et, eu, fa, fi, fr, fr-CA, fr-FR, gl, he, hi, hr, hu, id, is, it, ja, ja-JP, ka, km, kn, ko, lb, lo, lt, lv, mk, ml, mn, mr, ms, nb, ne, nl, nn, no, or, pa, pl, pl-PL, pt, pt-BR, rm, ro, ru, sk, sl, sq, sr, sv, sw, ta, te, th, tr, tt, ug, uk, ur, uz, vi, wo, zh, zh-CN, zh-Hans, zh-Hant

New chat