0

I have been trying to compute and set a flow variable using liquid template syntax. What I need to do is filter an array to remove a value from it and set that into a variable.

First I tried using where expression but it's not documented in Tiwlio docs, so my guess is it's not supported.

I assign body & options but in Studio Flow these would already defined values

Example code:

{% assign body = "Test2" %}
{% assign options = "Test,Test2,Test3" | split: ","  %}
{% assign options_filtered = "" | split: "" %}
{{options_filtered | where: body }}

Since the above attempt was unuseful, I tried populating the array manually and then joining it into a string

Example code:

{% assign body = "Test2" %}
{% assign options = "Test,Test2,Test3" | split: ","  %}
{% assign options_filtered = "" | split: "" %}
{% for item in options %}
 {% if item != body %}
  {% assign options_filtered = options_filtered | concat: item %}
 {% endif %}
{% endfor %}
{{options_filtered | join: ","}}

This approach was unsuccessful as well, I just get errors that returned value is not a JSON.

What I ended up doing is creating a Twilio Function where I pass a stringified array and value to filter out, but I would still like to avoid using the function if possible.

Any ideas and suggestions are welcome.

Thanks!

2 Answers2

2

I found this worked:

{% assign body = "Test2" %}
{% assign options = "Test,Test2,Test3" | split: "," %}
{% assign options_filtered = options | remove: body | join: "," %}
{{ options_filtered }}

Make sure you are only using options documented in Twilio's Liquid guide. For example, concat and where are not present. remove is the filter you need to filter a list.

philnash
  • 70,667
  • 10
  • 60
  • 88
0

@philnash

Well, this sort of works if the items in array all all different, but I have a problem if body is s substring in an option, like for example:

{% assign body = "About" %}
{% assign options = "About App,About Company,About Company Services" | split: "," %}
{% assign options_filtered = options | remove_first: body | join: "," %}
{{ options_filtered }}

Output would be

App,About Company,About Company Services