IF statement for one variable with multiple values

1.1k Views Asked by At

Is there a better way to write this IF statement for testing multiple options of a variable?

<#if PRINTER_PET.RETAILER_NAME = 'BEST BUY' || PRINTER_PET.RETAILER_NAME = 'Best Buy Purchasing LLC'>
document 1 
<#elseif  PRINTER_PET.RETAILER_NAME = 'AMAZON' || PRINTER_PET.RETAILER_NAME = 'Amazon Fulfillment Services Inc Attn: Amazon.com' >
document 2
</#if>

Thank you, Erica

2

There are 2 best solutions below

0
On

Follow Freemrker Comparison example and use == and ":

<#if PRINTER_PET.RETAILER_NAME == "BEST BUY" || PRINTER_PET.RETAILER_NAME == "Best Buy Purchasing LLC">
document 1 
<#elseif  PRINTER_PET.RETAILER_NAME == "AMAZON" || PRINTER_PET.RETAILER_NAME == "Amazon Fulfillment Services Inc Attn: Amazon.com" >
document 2
</#if>

The user == "Big Joe" expression in the <#if ...> will evaluate to the boolean true, so the above will say "It is Big Joe".

Logical or: ||

1
On

You can extract PRINTER_PET.RETAILER_NAME into a variable, like <#assign retName = PRINTER_PET.RETAILER_NAME>, and then it's somewhat shorter (<#if retName == 'foo' || retName == 'bar'>, etc.). Also, if you have a lot of strings to compare with, <#if ['foo', 'bar', 'baaz']?seq_contains(retName)> might be nicer.