SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

How to Use Liquid Template Expressions to Validate and Branch on Customer Input

Objective

This article explains how to use Liquid template expressions in Studio Flows to validate and branch on customer input, such as checking if a value is the correct length or matches a specific pattern. If you want to make decisions in your Studio Flow based on variables set by upstream widgets or Functions, Liquid templates provide a flexible way to access and evaluate those variables even when the Split widget UI does not allow direct reference to nested properties.

 

Product

Studio

 

Environment

Twilio Console

 

User Account Permission/Role(s) Required 

  • Access to the Twilio Console
  • Permission to edit Studio Flows

 

Procedure 

  1. Set Up Your Studio Flow
    • Create or open your Studio Flow in the Twilio Console.
    • Add widgets to collect or process customer input (e.g., Gather, Run Function, Set Variables).
  2. Understand Variable Scoping
    • Variables set by widgets are accessed as widgets.[widget_name].[property].
    • Variables set by Functions or Set Variables widgets may be available as flow.variables.[variable_name].
    • Some widgets (like Functions returning JSON) may nest values, e.g., widgets.myFunction.parsed.myProperty.
  3. Using Liquid Templates in Split Widgets
    • When configuring a Split widget, you may find the UI only allows top-level variables (e.g., widgets.isValidInput), not nested ones (e.g., widgets.isValidInput.status).
    • To work around this, use a Set Variables widget before the Split to extract or transform the value you need using a Liquid template.
  4. Example: Validating Input Length
    • Suppose you want to check if a customer’s input is exactly 6 digits.
    • Add a Set Variables widget before your Split.
    • In the Set Variables widget, create a new variable (e.g., digits_length) and set its value using a Liquid expression:
{{ widgets.gather_1.Digits | size }}
  • This will set digits_length with the length of the Digits value and you will be able to compare it in the Split On widget with an expected value (e.g. 6 digits).
  1. Branching on the Result
    • In your Split widget, reference the new variable (e.g., flow.variables.digits_length) to branch your flow based on whether the input is valid.
  2. Accessing Nested Properties from Functions
    • If your Function returns a JSON object, access nested properties using the parsed key:
{{ widgets.stripalpha.parsed.validationStatus }}
  • Use a Set Variables widget to extract this value if the Split widget does not allow direct reference.
  1. Advanced: String Manipulation and Substrings
    • You can use Liquid filters to extract substrings or check patterns:
      • To get the first 3 characters:
        {{ widgets.gather_1.Digits | slice: 0, 3 }}
      • To check length:
        {{ widgets.gather_1.Digits | size }}

 

Additional Information 

For further assistance, please reach out Twilio Support.

Have more questions? Submit a request
Powered by Zendesk