Using Tokens, Editing Templates & Custom Coding

Understanding Tokens, Loops & Functions

Some Actions in Arigato allow you to insert tokens, which represent the data available in the context of your workflow. For example, a product workflow will have product related tokens available to you, such as the product's name, or the product's tags. 

Tokens vs. Loops vs. Functions

Some tokens represent a single dynamic value, such as a product title. 

{{ product.title }}

Other times, the token browser will insert a code loop that allows you to access each item in a list, such as each variant on a product, or each line item in an order.

{% for variant in product.variants %}

{% endfor %}

In Arigato, we've also created a few functions for your convenience. Functions are a single line of code that, when evaluated in Arigato, will format an object in a certain way, saving you from tedious coding for common uses. An example of a function in Arigato is the product list function on an order. A call to this function will produce a complete product list with links and all common data points, for use in an email. See Functions below for more information.

Token Browser Example

The example below shows how to display a list of variant titles for a newly paid order workflow. The first step is to click on the token picker button:

Next, search for the variant title on the token picker, this will fall under "Line items":

Finally, click on the "Insert token" button as shown below:

Functions

There are some helpful built-in functions that make common formats more accessible. Functions have arguments that control how they work. For example, depending on whether you are sending an HTML email or a Slack message, the "html:true" argument will control whether the function renders HTML or as plain text. Each function is documented in the Token Browser with the arguments they accept.

The following example shows the "format_address()" function to format a complete address with street, company, city, state, and ZIP.

Using Twig Syntax

Twig is a templating engine (similar to Shopify's Liquid) that allows you to perform some basic string and array manipulation. Common uses are listed below. You may also read the Official Twig Documentation. See below for additional custom filters in Arigato Automation and filters that act similarly to Liquid.

Additional examples and references can be located in the Arigato app, under the Code Reference link.

Assigning Variables

{% set foo = "Bar" %}

For Loops

{% for item in array %} {{ item }} {% endfor %}

If Conditions

{% if some_value > 100 %} <!-- do the things --> {% endif %} {% if some_variable is not empty %} <!-- do the things --> {% endif %}

Split Strings

{% set tags = customer.tags|split(',') %}

Example Filters

{{ 9800.333 | number_format(2,'.',',') }} {{ order.created_at | date("F j, Y, g:i a - T") }} Example output: April 25, 2019, 2:17 pm - EDT

{{ "now" | date("m/d/Y") }}

View date formatter options.

{{ 'my first car'|capitalize }} Example output: My First Car {{ "I like %this% and %that%."| replace({'%this%':foo,'%that%':"bar"}) }}

Custom Filters in Arigato Automation

Twig does not natively provide a matching filter for all the Liquid filters you may be accustomed to. We've added support for these filters in Arigato Automation, and a few other handy filters. Note that Twig syntax is very similar but not identical to Liquid.

to_json

Converts any object or array to json. This is extremely handy when you need to dump all variables in an array to inspect them.

{{ order.line_items | to_json }}

from_json( $keyed_array: false )

Converts a JSON string into an object or keyed array.

{{ '{"num": 1}' | from_json.num }} // Prints "1" (object syntax) {% set json = '{"num": 1}' | from_json(true) %} {% set key = "num" %} {{ json[key] }} // Prints "1" (keyed array syntax)

to_handle

Convert any string into a handle.

{{ "my product title" | to_handle }}

strip_newlines

Removes any new line characters such as br tags or n tags.

Similar Liquid filter

{{ "two lines<br />of text" | strip_newlines }}

strip

Removes whitespace from the left and right side of a text string.

Similar Liquid filter

{{ " some text with whitespace " | strip }}

truncate

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.

Similar Liquid filter

{{ "This is some text" | truncate(5) }}

strip_html

Removes any HTML tags from a string.

Similar Liquid filter

{{ "<p>Some text <strong>here</strong></p>" | strip_html}}

append

Concatenates two strings and returns the concatenated value.

Similar Liquid filter

{{ "a/custom/url" | append(".php") }}

prepend

Adds the specified string to the beginning of another string.

Similar Liquid filter

{{ "Fix the thing" | prepend("To do:") }}

at_least

Sets a minimum value.

Similar Liquid filter

{{ 4 | at_least(5) }}

ceil

Rounds a number to the nearest whole number

Similar Liquid filter

{{ 1.2 | ceil }}

floor

Rounds a number down to the nearest whole number.

Similar Liquid filter

{{ 44.40 | floor }}

downcase

Makes all characters in a string lowercase.

Similar Liquid filter

{{ "Dan Pepin" | downcase }}

upcase

Makes all characters in a string uppercase.

Similar Liquid filter

{{ "John Carbone" | upcase }}

map

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

Similar Liquid filter

{% set all_categories = site.pages | map("category") %} 
{% for item in all_categories %}
 {{ item }} 
{% endfor %}

compact

Removes all nil values in an array.

Similar Liquid filter

{% set site_categories = site.pages | map('category') | compact %} 
{% for category in site_categories %}
 {{ category }} 
{% endfor %}

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.

Similar Liquid filter

{{ "Ground control to Major Tom." | truncatewords(3) }}

url_decode

Decodes a string that has been encoded as a URL.

Similar Liquid filter

{{ "%27Stop%21%27+said+Fred" | url_decode }}

where

Creates an array including only the objects with a given property value, or any truthy value by default.

Similar Liquid filter

{% set kitchen_products = products | where("type", "kitchen") %}

discount

Apply a discount as a percentage to a number.

{{ 100 | discount(20) }}

reverse_discount

Add a discount back to a numb

{{ 80 | reverse_discount(20) }}

divided_by

Divides a number by the specified number.

Similar Liquid filter

{{ 16 | divided_by(4) }}

plus

Adds one number to another number.

Similar Liquid filter

{{ 4 | plus(2) }}

minus

Subtracts one number from another number.

Similar Liquid filter

{{ 8 | minus(4) }}

times

Subtracts a number from another number.

Similar Liquid filter

{{ 4 | times(2) }}

raw

Prevents a value from being escaped and prints the raw value. This filter comes in handy when you don't want special characters to be converted to the HTML entity equivalent.

Similar liquid filter

{{ my_variable | raw }}

Coding Tips & Examples 

Inspecting Variables

Use the to_json filter to inspect variables and arrays. We recommend selecting an item to test with when building your workflow to inspect all variables in a user interface, and using this option as a last resort.

{{ your_variable | to_json }}

String Concatenation 

Example of string concatenation in a loop in Twig. Use the ~ character to join two strings together

{{ set reorder = '' }} {% for line_item in order.line_items %} {{ line_item.variant_id }} {{ line_item.quantity }} {% set reorder = reorder ~ line_item.variant_id ~ ":" ~ line_item.quantity ~ "," %} {% endfor %} https://{{ shop.domain }}/cart/{{ reorder }}

Getting the URL to a Featured Image

Example of printing the URL to a featured image (the first image on a product).

Print the source to the first image {{ product.images | first.src }} Save to a variable {% set featured_image = product.images | first %} Image URL: {{ featured_image.src }}

Checking if an array is empty

{% if order.discount_codes | length %}

Convert a flat array to a comma-separated list

{{ product.tags | join(', ')}}

Convert one item in an associative multi-dimensional array to a comma-separated list

The array, in this case, has several objects, each containing 3 data points: "code", "amount", and "type". We're only looking for the codes in this case. The end result would look like this: code1, code2, code3.

{{ order.discount_codes | map('code') | join(', ') }}

Strip certain HTML tags

This series of filters ensure that the rendered HTML is not HTML encoded. Add the tags you want to preserve.

{{ product.body_html | striptags('<br><p>') | raw }}

Truncating Data

You may wish to only store the beginning of your product's description for use in another system, such as Google Shopping. Our example would output only the first 30 characters of your product description.

{{ product.body_html | slice(0, 30) }}