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
-
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).
-
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.
- Variables set by widgets are accessed as
-
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.
- When configuring a Split widget, you may find the UI only allows top-level variables (e.g.,
-
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_lengthwith 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).
-
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.
- In your Split widget, reference the new variable (e.g.,
-
Accessing Nested Properties from Functions
- If your Function returns a JSON object, access nested properties using the
parsedkey:
- If your Function returns a JSON object, access nested properties using the
{{ widgets.stripalpha.parsed.validationStatus }}- Use a Set Variables widget to extract this value if the Split widget does not allow direct reference.
-
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 }}
- To get the first 3 characters:
- You can use Liquid filters to extract substrings or check patterns:
Additional Information
- For more on Liquid templates in Studio, see Twilio Studio Liquid Template Language.
- For a practical example, see Creating Time-of-Day Routing in Twilio Studio Using Liquid Variables.
- If you encounter issues referencing variables in Split widgets, use a Set Variables widget to create a top-level variable for branching.
For further assistance, please reach out Twilio Support.