Onit Documentation

Crash Course on Liquid

by Michael Nadeau Updated on

Liquid is an open-source markup language that can expand your App’s functionality.

Liquid can help you:

  • Format data: If you have a number in a Field (e.g., 1000), you could format it as a currency value (e.g., $1,000.00) using Liquid. This might be necessary when using the number in an email or report.
  • Perform Math: You could add two Fields together to calculate and populate the value of a third Field.
  • Make Decisions: Liquid can help you integrate decision-making into your App. For example, you can create “if/then” statements to define how your App should behave in various situations.
  • A whole lot more! The examples above barely scratch the surface. Once you learn Liquid’s basics, you’ll find many opportunities to expand your App’s functionality.

If you’ve never heard of Liquid before, don't worry! Learning the basics isn’t difficult and does not require any programming knowledge. In many cases, you can achieve a great deal in an App with only a limited understanding of Liquid.

In this tutorial, we’ll cover Liquid’s basic concepts and syntax.

Where can I use Liquid?

Before learning Liquid, it’s essential to understand where it can be used within an App.

Roughly speaking, Liquid can be used anywhere you want to return a value or perform decision-making. For example, when creating Conditions, UI Actions, filters, and Field Calculations — among many other things.

When configuring an App, you’ll often see text boxes with an Editor button in the top-right corner (as in the screenshot below). This indicates that you can use Liquid within the text box. The Liquid you insert into a textbox will be executed when your App runs.

The screenshot below shows how Liquid can be used to build a condition.

Clicking the Editor button (shown above) will open the Liquid Editor, a tool that helps you build and validate your Liquid. This editor also shows you a list of the available variables you can access and manipulate using Liquid.

Though most do, not all Fields that allow Liquid have an Editor button. Sometimes, you can use Liquid in a textbox that does not have this button.

Object vs. Tag

There are two basic types of Liquid markup: object and tag.

Object

An object looks like this:

{{ expression }}

The result of the expression will be printed/displayed wherever the Liquid is being used.

For example:

{{ customer_name }} 

This Liquid says, “In the location where this Liquid markup appears, insert the customer’s name.” In this example, customer_name is a Field within your App. The exact customer name that will be printed/displayed depends on which Record is running.

Tag

The second type of Liquid markup is called a tag, which uses different surrounding characters.

Tags looks like this:

{% logic %}

Tags are used to perform decision-making. Tags don’t print/display anything on their own — they run logic.

For example:

{% if customer_name == 'Acme' %} do something {% endif %}

In this example, two separate tags are used: an opening tag and a closing tag. This Liquid means, “Only do something if the customer’s name is Acme.”

The example above uses an “if/then” statement, which you’ll learn more about below. What’s important to understand right now is that logic goes inside of tags.

You can also use objects within tags. For example:

{% if X == Y %} <= Tag

  {{ field_name }} <= Object

{% endif %} <= Tag

This Liquid says, “Only print/display field_name if X equals Y.”

Piping Data

Very often, you will need to retrieve a value from somewhere and then modify it using Liquid. To do so, you will frequently use the pipe character, which looks like this: |. This character is usually above the enter key.

The pipe character essentially means, “Take the value that lives on my left and pass it to the expression that lives on my right.”

For example:

{{ price | currency }}

This Liquid means, “Get the value of the price Field, and then pass it to the currency filter so that it gets formatted like a currency (e.g., $315.67).”

Here’s another example:

{{ now | date: “%y” }}

This Liquid means, “Take whatever the date/time is right now, and pass that to the date filter so that only a year is printed/displayed (e.g., 2018)."

In both examples above, it’s not important that you understand the currency or date filters, or the now keyword. The key concept here is how one value is being passed to the right, so that it can be modified.

The examples above each contain only one pipe character. Note though that you can pipe as many times as necessary within a single expression. For example:

{{ variable | expression1 | expression2 | expression3 }}

Decision Making

Often, an App should make decisions based on a set of predefined rules. A fundamental example would be: if a Field is greater than a certain amount, do something.

Liquid can help you define decision-making logic using “if/then” blocks, which look like this:

{% if field > X %}

  Do something

{% elsif field > Y %}

  Do a different thing Y

{% else %}

  Do yet a different thing

{% endif %}

It’s important to note that not all “if/then” statements will have this exact structure. That’s because certain elements shown above are optional. If you don’t need them, you can remove the elsif and else sections. The only requirement is to have a starting if and an ending endif.

You’ll have many opportunities to use “if/then” statements when using Apps. It’s common to use them when creating conditions, calculating Fields, and building UI actions, amongst other things.

When using “if/then” statements, you’ll want to be aware of the various operators that Liquid provides. Examples of operators include > (greater than) and == (equal to). A list of operators is available in the official Liquid documentation.

In addition to “if/then” statements, Liquid also offers a “case/when” statements, which can help you build decision making logic.

Arrays

Most of the time, an App will present its data as single-valued variables. For example, a Record’s customer_name Field contains only one value: a customer’s name.

However, sometimes, you’ll need to work with a special type of variable that contains more than one value (at the exact same time). These special variables are called arrays.

For example, an App returns a variable named current_user, which contains various properties about (you guessed it!) the current user. Because a user has multiple properties (like a name and an email address), the current_user variable is an array.

To make things even more interesting, there are different types of arrays. How you work with any given array will depend on its type.

One type of array contains a single series of data. That’s an abstract statement, so let’s look at an example.

The current_user array contains data about a single user, not multiple users. As a result, current_user has only one name value because humans have only one name (well, usually). So, current_user is a single-series array.

To work with arrays that contain a single series of data, we can use the following syntax:

{{ array_name.value_you_want }}

Note the period character (.), which you place between the array’s and variable’s names. For example: {{ current_user.name }} might return John Doe. In this example, name is a variable that lives inside of the current_user array.

Alternatively, another type of array contains multiple series of data.

For example, a Record’s histories variable contains metadata about each of the significant events that have occurred in the life of a particular Record. Because multiple events can occur, histories usually contains multiple series of data. As a result, we can’t work with this array the same way we would a single-series array. Instead, we’ll need to work with something called a “loop.”

Loops

Liquid offers a “for loop” as a way to “loop” over an array. Within the loop, you can interact with each series of data (one by one) and then extract (or act on) the specific pieces of data you care about.

In the example below, we loop over histories, find the event attributed to a certain user, and then “do something” with that specific data series.

{% for event in histories %}

  {% if event.actor == '[email protected]' %}

    Do something 

   {% break %}

  {% endif %}

{% endfor %}

This Liquid essentially says, “Loop over histories (which is an array) and then find the first event performed by a specific user (based on their email address). Once you find it, do something.”

The {% break %} command (which is optional) tells your Liquid to stop looping once the “if/then” statement is satisfied. In other words, once you find the first event performed by the user in question, don’t worry about any other events -- stop looping. If you removed the “break” statement, Liquid would keep looping, even after finding the first (and any subsequent) events performed by this particular user.

Next Steps

Nice work! You’re now familiar with the basics of Liquid. Depending on the complexity of the Apps you plan to build, you may need to learn about various other Liquid concepts. Luckily, Liquid is a well-documented tool. We recommend checking out the following resources:

Additionally, you may be interested in learning how to use Onit's Liquid Editor, which can help you write and preview your Liquid scripts. See our Using the Liquid Editor tutorial for more information. 

Once you have grasped the basics of liquid, you might also want to check out our Liquid Examples article, which can inspire you to explore all the ways Liquid can augment workflows in Onit.

Previous Article Building Your First App
Next Article Using the Liquid Editor

© 2024 Onit, Inc.

docs.onit.com contains proprietary and confidential information owned by Onit, Inc. that is subject to copyright. Onit presents it exclusively to you for your sole use in conjunction with using Onit products. No portion of the materials contained herein may be used for any other purpose. No portion of the materials contained herein may be shared with third parties or reproduced in any form.