Liquid [DELETED]

Overview 

Tines supports the use of the Liquid templating engine in Action options blocks. Liquid provides a number of useful filters which can be used to manipulate the contents of options blocks. For example, the expression {{ 'hello' | upcase }} uses the upcase filter, when inserted into an options block it will render HELLO. You can string multiple Liquid filters together, with the expression processed left-to-right.

Examples 

Get the Current Date and Time in yyyymmdd Format 

Expression: {{ "now" | date: "%Y%m%d" }}

Sample result: "20180424"

Reference

Get the Size of an Array from an Incoming Event 

Expression: {{ .my_array | size }}

Sample result: 5

Reference

Multiply a Number in an Incoming Event by 10 

Expression: {{ .my_number | times: 10 }}

Sample result: 50

Reference

Get the Size of an Array and Multiply by 10 

Expression: {{ .my_array | size | times: 10 }}

Sample result: 50

Reference

Full List of Available Filters 

  • abs: Returns the absolute value of a number.

  • append: Concatenates two strings and returns the concatenated value.

  • as_object: Cast data as an object instead of a string. Useful with other Liquid operations and for sending/receiving data.

  • at_least: Limits a number to a minimum value.

  • at_most:Limits a number to a maximum value.

  • base64_encode: Encode a string using the Base64 encoding algorithm.

  • base64_decode: Decode a Base64 encoded string.

  • base64url_encode: Encode a string to a URL-safe base64url encoded string.

  • base64url_decode: Decode a URL-safe base64url encoded string.

  • capitalize: Makes the first character of a string capitalized.

  • ceil: Rounds the input up to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.

  • compact: Removes any nil values from an array.

  • concat: Concatenates (joins together) multiple arrays. The resulting array contains all the elements from the input arrays.

  • csv_parse: Parse a CSV-formatted string with auto-detection of the delimiter character. Use with as_object, e.g.: {{ .csv_string | csv_parse | as_object }}

  • date: Converts a timestamp into another date format. The format for this syntax is the same as strftime.

  • default: Allows you to specify a fallback in case a value doesn’t exist. default will show its value if the left side is nil, false, or empty.

  • divided_by: Divides a number by the specified number.

  • downcase: Makes each character in a string lowercase. It has no effect on strings which are already all lowercase.

  • eml_parse: Takes a string containing EML content (i.e.: RFC822) and parses out information such as tofromsubjectattachments, etc. Use with as_object, e.g.: {{ .raw_eml_string | eml_parse | as_object }}

  • escape: Escapes a string by replacing characters with escape sequences (so that the string can be used in a URL, for example). It doesn’t change strings that don’t have anything to escape.

  • escape_once: Escapes a string without changing existing escaped entities. It doesn’t change strings that don’t have anything to escape.

  • first: Returns the first element of an array.

  • floor: Rounds a number down to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.

  • get: Get the value at the supplied path. e.g.: {{ .my_object | get: "my.path" }}

  • hmac_sha1: Converts a string into a SHA-1 hash using a hash message authentication code (HMAC). Pass the secret key for the message as a parameter to the filter.

  • hmac_sha256: Converts a string into a SHA-256 hash using a hash message authentication code (HMAC). Pass the secret key for the message as a parameter to the filter.

  • hmac_sha256_base64: Converts text into a SHA-256 hash using a hash message authentication code (HMAC) and returns the data in base64. Pass the secret key for the message as a parameter to the filter.

  • html_encode: Escapes HTML syntax characters in a string, e.g. "<élan>" becomes "&lt;élan&gt;".

  • html_decode: Converts escaped characters in a string to HTML syntax characters, e.g. "&lt;élan&gt;" becomes "<élan>".

  • in_cidr: Checks if an IP address is in a given CIDR block, e.g.: {{ .ip_address | in_cidr: '10.0.0.0/8' }}

  • is_ip_address: Checks if text is a valid IP address, e.g.: {{ '10.2.3.4' | is_ip_address }}

  • join: Combines the elements in an array into a single string using the argument as a separator.

  • json_parse: Parse an escaped JSON string. Use with as_object, e.g.: {{ .escaped_json_string | json_parse | as_object }}

  • jsonpath: Evaluate complex JSONPaths. Includes support for wildcards and filters, e.g.: {{ .receive_event | jsonpath: '$.phoneNumbers[*].type' | as_object }}

  • jwt_sign: Create a JSON Web Token from the input claim set using either RS256 (default) or HS256, e.g.: {{ .claim_set | jwt_sign: .CREDENTIAL.jwt_rsa_private_key }}{{ .claim_set | jwt_sign: .CREDENTIAL.jwt_hmac_key, 'HS256', .extra_headers }}

  • last: Returns the last element of an array.

  • lstrip: Removes all whitespace (tabs, spaces, and newlines) from the beginning of a string. The filter does not affect spaces between words.

  • map: Creates an array of values by extracting the values of a named property from another object.

  • md5: Calculates the hex encoded md5 hash of a string.

  • md5_base64: Calculates the base64 encoded md5 hash of a string.

  • minus: Subtracts a number from another number.

  • modulo: Returns the remainder of a division operation.

  • neat_json: Format and "pretty print" an object in JSON.

  • newline_to_br: Replaces every newline (\n) with an HTML line break (\<br>).

  • pluralize: Output the singular or plural version of a string based on the value of a number, e.g.: {{ counter | pluralize 'item' }} outputs item or items, depending on the value of counter. For custom pluralization, you can pass a second argument, e.g. {{ 5 | pluralize 'bonus' 'bonuses'}} outputs bonuses.

  • plus: Adds a number to another number.

  • prepend: Adds the specified string to the beginning of another string.

  • random_element: Select a random element from an array.

  • regex_replace: Replaces every occurrence of a regex match in a string with the second argument, e.g. {{ "hello hello!" | regex_replace: "h\w+", "goodbye" }} outputs goodbye goodbye!.

  • remove: Removes every occurrence of the specified substring from a string. Removes the elements from an array if the expression is terminated with as_object e.g. {{ .array | remove: '<element>' | as_object }}.

  • remove_first: Removes only the first occurrence of the specified substring from a string. Removes the element from an array if the expression is terminated with as_object e.g. {{ .array | remove_first: '<element>' | as_object }}.

  • remove_key: Removes the specified key from an object e.g. {{.http_request_action | remove_key: 'body.hash_example.key' | as_object }}.

  • replace: Replaces every occurrence of an argument in a string with the second argument.

  • replace_first: Replaces only the first occurrence of the first argument in a string with the second argument.

  • reverse: Reverses the order of the elements in an array. reverse cannot reverse a string.

  • round: Rounds an input number to the nearest integer or, if a number is specified as an argument, to that number of decimal places.

  • rstrip: Removes all whitespace (tabs, spaces, and newlines) from the right side of a string.

  • sha1: Calculates the sha1 hash of a String.

  • sha256: Calculates the sha256 hash of a String.

  • sha512: Calculates the sha512 hash of a String.

  • size: Returns the number of characters in a string or the number of elements in an array.

  • slice: Returns a substring of 1 character beginning at the index specified by the argument passed in. An optional second argument specifies the length of the substring to be returned.

  • sort: Sorts elements in an array by a property of an element in the array. The order of the sorted array is case-sensitive.

  • sort_natural: Sorts elements in an array by a property of an element in the array.

  • split: Divides an input string into an array using the argument as a separator. split is commonly used to convert comma-separated items from a string to an array. Use with as_object to send data as an array, e.g.: {{ .comma_delimited_string | split: ',' | as_object }}. Without using as_object, the result will commonly be a string with concatenated values in the array.

  • strip: Removes all whitespace (tabs, spaces, and newlines) from both the left and right side of a string. It does not affect spaces between words.

  • strip_html: Removes any HTML tags from a string.

  • strip_newlines: Removes any newline characters (line breaks) from a string.

  • times: Multiplies a number by another number.

  • to_csv: Format an array into a CSV-formatted string.

  • to_json: Format an object as JSON.

  • to_snake_case: Turn input string into snake case.

  • transliterate: Replaces non-ASCII characters with an ASCII approximation, or if none exists, a replacement character: “?”.

  • truncatetruncate shortens a string down to the number of characters passed as a parameter. If the number of characters specified is less than the length of the string, an ellipsis (…) is appended to the string and is included in the character count.

  • truncatewords: Shortens a string down to the number of words passed as the argument. If the specified number of words is less than the number of words in the string, an ellipsis (…) is appended to the string.

  • type: Outputs the class of a specified object, e.g.: String or Array.

  • uniq: Removes any duplicate elements in an array.

  • unzip: Extracts files from a ZIP archive. Use with as_object, e.g.: {{ .zip_data | unzip: 'optional_password' | as_object }}

  • upcase: Makes each character in a string uppercase. It has no effect on strings which are already all uppercase.

  • url_decode: Decodes a string that has been encoded as a URL or by url_encode.

  • url_encode: Converts any URL-unsafe characters in a string into percent-encoded characters.

  • where: Selects all the elements in an array where the key has the given value. {{ .get_all_alarms.body.alarms | where:"classification","malware" }}

  • zip: Creates a ZIP archive containing a given file. If the optional password parameter is present then the archive will be password protected. {{ .stream_to_zip | zip: 'optional_filename', 'optional_password' }}

Logical Operators 

Liquid provides a number of logical operators to interact with data. This allows for complex operations to be carried out within the options of a Tines action.

If/Else 

Data:

{ "customer": { "name": "anonymous" } }

Expression:

{% if customer.name == "kevin" %}Hey Kevin!{% elsif customer.name == "anonymous" %}Hey Anonymous!{% else %}Hi Stranger!{% endif %}

Sample result: Hey Anonymous!

If/Else with Contains 

Note: contains is case-sensitive

Data:

{ "collection": { "products": ["hat", "shirt", "pants"] } }

Expression:

{% if collection.products contains "hat" %}Collection has a hat{% else %}No hat found{% endif %}

Sample result: Collection has a hat

If/Else with Contains (text) 

Note: contains is case-sensitive

{ "customer": { "address": "1 High Street, Downtown" } }

Expression:

{% if customer.address contains "Street" %}Address has a street{% else %}No street{% endif %}

Sample result: Address has a street

For Loop 

Data:

{ "collection": { "products": ["hat", "shirt", "pants"] } }

Expression:

{% for product in collection.products %}{{ product.title }}{% endfor %}

Sample result: hat shirt pants

Convert String to Integer 

{{ "1" | minus: 0 }}

Convert Integer to String 

{{ 1 | append: "" }}

Variables 

Assign 

assign creates a new variable by direct assignment at runtime that allows for storing data, commonly after performing manipulation operations.

In this example, assign can be used with the Liquid filter minus to calculate, store, and use my_variable in a logical operation.

Input:

{% assign my_variable = 2 | minus: 1 %}
{% if my_variable == 1 %}
  This statement is valid.
{% endif %}

Output:

This statement is valid.

Capture 

capture creates a new variable containing the result of anything within capture tags.

Input:

{% capture my_variable %}I am being captured.{% endcapture %}
{{ my_variable }}

Output:

I am being captured.

More Information 

A full list of Liquid filters and operators are available in the Shopify documentation.

Was this helpful?