Liquid is a template language used to create dynamic content. It is used within Twilio's Studio product for simple manipulations of data from other widgets (for more advanced data manipulation, the "Run Function" widget allows users to write business logic that works alongside Studio Widgets). This guide presents some common use cases for Liquid within Studio.
Build an iterator for a loop
Note: A Studio variable created with the "Set Variables" will be numeric if you do not enclose it in quotation marks.
- Sometimes, you might need to handle numeric content that was created in quotation marks. For example,
"1"
is a non-numeric string field within Studio. This type of input could be expected if you are parsing an incoming body of a text message. - If you add an integer to a string in Liquid, it will automatically convert the sum into an integer.
Loop through JSON Array
The above iterator will be helpful for advance use case where multiple widgets needs to be executed repeatedly. But for simple looping inside a single widget Liquid's for-loop can be used instead.
{% for item in widgets.<widget_name>.parsed.result %}
{{ item.<json-key> }}. {% comment %} item variable will have array element in each iteration {% endcomment %}
{% endfor %}
This will be useful when a HTTP Request returns a JSON Array, which needs to be iterated and used inside a single widget. In below flow array for iterated to say in Say/Play widget.
{% for i in widgets.http_1.parsed.result %}
{{ i.question }}
{% endfor %}
Handling date and time strings
Note: When you're using Liquid time and date templates in Studio, the timezone used is PST.
You can use the format tokens listed below:
%a - The abbreviated weekday name ("Sun")
%A - The full weekday name ("Sunday")
%b - The abbreviated month name ("Jan")
%B - The full month name ("January")
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%e - Day of the month (1..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%k - Hour of the day, 24-hour clock (0..23)
%l - Hour of the day, 12-hour clock (0..12)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator ("AM" or "PM")
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal "%" character
For example, the following would provide the current year followed by the current month and day: