Skip to content

Variables

Use variables to store and manipulate data within your templates. This includes assigning values, incrementing and decrementing numeric variables, and capturing output into a variable.

Functionality

  • Assigning Variables: Stores a specific value in a variable.
  • Incrementing Variables: Increases the value of a numeric variable by 1.
  • Decrementing Variables: Decreases the value of a numeric variable by 1.
  • Capturing Output: Captures the output of a block of code into a variable.

Syntax

Assigning Variables

You use the assign method to store values in a variable.

    {% assign variable_name = value %}
Incrementing Variables

Use the increment method to increase a numeric variable by 1.

    {% increment variable_name %}
Decrementing Variables

Use the decrement method to decrease a numeric variable by 1.

    {% decrement variable_name %}
Capturing Output

The capture method captures the rendered output of Liquid code into a variable.

    {% capture variable_name %}
        Liquid code here
    {% endcapture %}
Arguments

These methods do not require any additional arguments.

Code Samples

Example 1: Assigning a Variable

    {% assign name = "Experience Builder" %}
    {{ name }}
Output:
    Experience Builder
Example 2: Incrementing a Variable
    {% assign counter = 0 %}
    {% increment counter %}
    {{ counter }}
Output:
    1
Example 3: Decrementing a Variable
    {% assign counter = 3 %}
    {% decrement counter %}
    {{ counter }}
Output:
    2
Example 4: Capturing Output into a Variable
    {% capture message %}
        Hello, {{ name }}!
    {% endcapture %}
    {{ message }}
Output:
    Hello, Experience Builder!

Outliers

  • Non-Numeric Increment/Decrement: Applying increment or decrement to non-numeric variables will not work as expected and may produce an error or unexpected results.
  • Reassigning Variables: Reassigning a variable will overwrite the old value with the new value.
  • Scope Limitations: Variables assigned within a block may not be accessible outside of that block, depending on the scope.

Key Points

  • Assignments are used for storing values into variables for later use within your Experience Builder pages.
  • Increment and decrement methods are useful for counting or iterating within loops.
  • Capture is ideal for creating complex strings or storing output from multiple Liquid statements.