Skip to content

Operators

Experience Builder's Liquid supports a wide range of operators, allowing for advanced functions and calculations within Liquid resources. List of Operators

  1. Arithmetic Operators

  2. Logical Operators

  3. Comparison Operators

  4. String Operators

Arithmetic Operators

Addition (+)

  • Syntax: left + right
  • Adds two numbers.

Example:

    {% assign a = 10 %}
    {% assign b = 5 %}
    Sum: {{ a | plus: b }}
Output:
    Sum: 15
Key Points:

  • Works with Number values.
  • Implicit conversion may occur when dealing with String values.

Subtraction (-)

  • Syntax: left - right
  • Subtracts the right number from the left.

Example:

    {% assign a = 10 %}
    {% assign b = 5 %}
    Difference: {{ a | minus: b }}
Output:
    Difference: 5
Key Points:

  • Ensure both operands can be converted to Number values.

Multiplication (*)

  • Syntax: left * right
  • Multiplies two numbers.

Example:

    {% assign a = 10 %}
    {% assign b = 5 %}
    Product: {{ a | times: b }}
Output:
    Product: 50
Key Points:

  • Use NumberValue for operations requiring precision.

Division (/)

  • Syntax: left / right
  • Divides the left number by the right.

Example:

    {% assign a = 10 %}
    {% assign b = 5 %}
    Quotient: {{ a | divided_by: b }}
Output:
    Quotient: 2
Special Cases:

  • Division by zero returns Nil value.

Modulus (%)

  • Syntax: left % right
  • Returns remainder of the division.

Example:

    {% assign a = 10 %}
    {% assign b = 3 %}
    Remainder: {{ a | modulo: b }}
Output:
    Remainder: 1
Special Cases:

  • Only applicable if both operands are integers.

Floor

  • Syntax: {{ [value] | floor }}
  • Rounds down to the nearest integer.

Example:

    {% assign a = 10.75 %}
    Floor Value: {{ a | floor }}
Output:
    Floor Value: 10

Ceil

  • Syntax: {{ [value] | ceil }}
  • Rounds up to the nearest integer.

Example:

    {% assign a = 10.25 %}
    Ceil Value: {{ a | ceil }}
Output:
    Ceil Value: 11

Logical Operators

And

  • Syntax: [value1] and [value2]
  • Logical AND operation.

Example:

    {% assign a = true %}
    {% assign b = false %}
    Result: {{ a and b }}
Output:
    Result: false

Or

  • Syntax: left or right
  • Logical OR operation.

Example:

    {% assign a = true %}
    {% assign b = false %}
    Result: {{ a or b }}
Output:

Result: true

Not

  • Syntax: not value
  • Logical NOT operation.

Example:

    {% assign a = true %}
    Result: {{ not a }}
Output:
    Result: false
Special Cases:

  • Works with any Boolean value.

Comparison Operators

Equal

  • Syntax: left == right
  • Checks if values are equal.

Example:

    {% assign a = 10 %}
    {% assign b = 10 %}
    Result: {{ a == b }}
Output:
    Result: true

Not Equal

  • Syntax: left != right
  • Checks if values are not equal.

Example:

    {% assign a = 10 %}
    {% assign b = 5 %}
    Result: {{ a != b }}
Output:
    Result: true

Greater Than

  • Syntax: left > right
  • Checks if left value is greater than right value.

Example:

    {% assign a = 10 %}
    {% assign b = 5 %}
    Result: {{ a > b }}
Output:
    Result: true

Less Than

  • Syntax: left < right
  • Checks if left value is less than right value.

Example:

    {% assign a = 10 %}
    {% assign b = 15 %}
    Result: {{ a < b }}
Output:
    Result: true

Greater Than or Equal

  • Syntax: left >= right
  • Checks if left value is greater than or equal to right value.

Example:

    {% assign a = 10 %}
    {% assign b = 10 %}
    Result: {{ a >= b }}
Output:
    Result: true

Less Than or Equal

  • Syntax: left <= right
  • Checks if left value is less than or equal to right value.

Example:

    {% assign a = 5 %}
    {% assign b = 10 %}
    Result: {{ a <= b }}
Output:
    Result: true

Contains

  • Syntax: left contains right
  • Checks if left value contains the right value.

Example:

    {% assign text = "Hello, World!" %}
    Result: {{ text contains "Hello" }}
Output:
    Result: true
Special Cases:

  • Works with strings and arrays only.

String Operators

Append

  • Syntax: left | append: right
  • Appends the right string to the left string.

Example:

    {% assign text = "Hello" %}
    Append: {{ text | append: ", World!" }}
Output:
    Append: Hello, World!

Prepend

  • Syntax: left | prepend: right
  • Prepends the right string to the left string.

Example:

    {% assign text = "World!" %}
    Prepend: {{ text | prepend: "Hello, " }}
Output:
    Prepend: Hello, World!

Slice

  • Syntax: slice(value, offset, length)
  • Extracts substring from the specified offset with an optional length.

Example:

    {% assign text = "Hello, World!" %}
    Slice: {{ text | slice: 7, 5 }}
Output:
    Slice: World

Remove

  • Syntax: remove(value, substring)
  • Removes the first instance of substring.

Example:

    {% assign text = "Hello, World!" %}
    Remove: {{ text | remove: "Hello, " }}
Output:
    Remove: World!

Replace

  • Syntax: replace(value, old, new)
  • Replaces all instances of old with new.

Example:

    {% assign text = "Hello, World!" %}
    Replace: {{ text | replace: "World", "Universe" }}
Output:
    Replace: Hello, Universe!

Key Points

Implicit Conversions

It's crucial to note that implicit conversions can occur, especially when adding numbers and strings. For instance, adding a String and Number could lead the result to default to a String.

Nil and Null

Operations involving Nil usually return Nil, particularly in division and comparison.

Boolean Context

In conditional statements, Empty, Blank, and False behave similarly by returning a False context.