SOLVED

Re: Velocity: Match value against list of values

Go to solution
lukea
Level 3

Velocity: Match value against list of values

Hi community,

In an email script/Velocity, what's the best way to anti-match a single value against a list of values in an if clause with several conditions?


For example: I could do

 

#set( $country = 'United States' )
#set( $list = ['United States','United Kingdom'] )
#if( !$list.contains($country) && 1 == 1 )
Salve,
#else
Dear friend,
#end

 

or

 

#set( $country = 'Italy' )
#if( !$country.matches('^(United States|United Kingdom)$') && 1 == 1 )
Salve,
#else
Dear friend,
#end

 


However, I am looking for a more direct and faster way in the veins of

 

#if( !['United States','United Kingdom'].contains($country) && 1 == 1 )

 

or something similar. 

As I came across this question a few times already, I thought I'd ask.

Thank you.

Tags (3)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity: Match value against list of values

The 1st method is preferred. If you’re desperate for a one-liner you can do

#if( !$display.alt(["United States","United Kingdom"]).contains($country) )

but it’s best avoided because that extra wrapper ends up losing clarity IMO.

View solution in original post

1 REPLY 1
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity: Match value against list of values

The 1st method is preferred. If you’re desperate for a one-liner you can do

#if( !$display.alt(["United States","United Kingdom"]).contains($country) )

but it’s best avoided because that extra wrapper ends up losing clarity IMO.