CMS Development

DM2
Member

Checking for multiple values in an if statement

SOLVE

Hi 

 

A basic hubl question...I want to check to see if a variable is one of several different values. In the sample below I'm looking to see if my_variable is equal to any of the 5 values listed.

 

Can anyone tell me why this doesn't work please:

 

{% if my_variable == ('string1', 'string2', 'string3', 'string4', 'string5') %}

Do something

{% endif %}

 

Appreciate any help, thank you.

 

0 Upvotes
1 Accepted solution
stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

Checking for multiple values in an if statement

SOLVE

@DM2 a sequence should use square brackets e.g. ['string1','string2]

 

Using the "is containing" expression you should be able to do what you're looking for. Here's how it works:

{% if [1, 2, 3, 4] is containing my_variable  %}
 variable is in sequence
{% endif %}

more info here: https://developers.hubspot.com/docs/cms/hubl/operators-and-expression-tests#expression-tests

 

Stefen Phelps, Community Champion, Kelp Web Developer

View solution in original post

2 Replies 2
stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

Checking for multiple values in an if statement

SOLVE

@DM2 a sequence should use square brackets e.g. ['string1','string2]

 

Using the "is containing" expression you should be able to do what you're looking for. Here's how it works:

{% if [1, 2, 3, 4] is containing my_variable  %}
 variable is in sequence
{% endif %}

more info here: https://developers.hubspot.com/docs/cms/hubl/operators-and-expression-tests#expression-tests

 

Stefen Phelps, Community Champion, Kelp Web Developer
DM2
Member

Checking for multiple values in an if statement

SOLVE

Thanks Stefen that worked perfectly first time!