IF statement for one variable with multiple values - freemarker

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

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.

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: ||

Related

How to Write Series in FreeMarker?

I am trying to write a Numeric and Alphabetic Series in Free-marker. However I am not able to implement it.
I have tried various portal and Freemarker website itself, but was not able to find a proper solution.
<#assign count = 0>
<#assign seq = ['a','b','c','d','e','f',]>
<#list params_list as test_param>
${count} ${seq[count]}
<#assign count = count + 1>
</#list>
It will print data in
1 a
2 b
3 c
You can use ?lower_abc (or ?upper_abc) to convert a number to a letter, where 1 corresponds to letter "a". If this is inside #list, then you can get the 1-based item counter with itemVariable?counter. For example:
<#list items as item>
${item?counter} ${item?counter?lower_abc}
</#list>

Count the value from the list

I'm setting up a synesty flow and I need to know how many ArticleNumber in the ArticleNumber list.
E.g
Here are the ArticleNumber list
35361,35361,35361,205,09308943528,093089435281,093089435281
I want to know how many ArticleNumber "09308943528" in the list
I've tried the ?contains but its only a boolean value.
<#if MainArticleNumberList!?contains('${ArticleNumber!}')>true<#else>false</#if>
I expect the output number depends on how many ArticleNumber in the list, in the example above it will show 1.
Use filter with size to calculate count:
${MainArticleNumberList?filter(x -> x=="09308943528")?sequence?size}
Use seq_contains freemarker builtin for sequences:
<#if MainArticleNumberList?seq_contains("09308943528")>true<#else>false</#if>
Tells if the sequence contains the specified value (according the == operator of the template language, not according Java's Object.equals). It has 1 parameter, the value to find.
If you are before FreeMarker 2.3.29, and thus can't use ?filter, you can still do this:
<#assign cnt = 0>
<#list MainArticleNumberList as articleNumber>
<#if articleNumber == '09308943528'>
<#assign cnt++>
</#if>
</#list>
${cnt}
out = [1,2,3,1,1,1]
print(out.count(1))
code:
[#assign numbers = [1,1,2,3,4,4,0,5,0,6,0,8,9]]
[#assign words = ["hello","bye","hello"]]
[#function getOccurrencesCount sequence item]
[#local occurrencesCount = 0]
[#list sequence as i]
[#if i == item]
[#local occurrencesCount++]
[/#if]
[/#list]
[#return occurrencesCount]
[/#function]
0 in numbers: ${getOccurrencesCount(numbers,0)}
6 in numbers: ${getOccurrencesCount(numbers,6)}
9 in numbers: ${getOccurrencesCount(numbers,9)}
"hello" in words: ${getOccurrencesCount(words,"hello")}
"bye" in words: ${getOccurrencesCount(words,"bye")}
output:
0 in numbers: 3
6 in numbers: 1
9 in numbers: 1
"hello" in words: 2
"bye" in words: 1

freemarker if statement sequencing

I'm trying to write an #if statement with a sequence of numbers. Basically, if a certain field matches any of a subset of numbers (shown below with || or operators) then assign it as "bayarea", elseif a different subset, then a different name, etc. Is this possible without a bunch of nested "or" statements?
I'm getting a syntax error saying that it's expecting a boolean yes/no statement.
<#if TEST_CONTACTS_LIST.PREFERRED_STORE ==
{12||21||22||38||46||67||71||74||76||77||83||86||104||113||119||143>
{bayarea}
<#elseif TEST_CONTACTS_LIST.PREFERRED_STORE ==
{34||62||84||91||137||144||152||169}>
{blueridge}
<#elseif TEST_CONTACTS_LIST.PREFERRED_STORE ==
{18||44||49||50||61||68||121||182}>
{frontrange}
<#else>
</#if>
You don't need nesting:
<#if TEST_CONTACTS_LIST.PREFERRED_STORE == 12
|| TEST_CONTACTS_LIST.PREFERRED_STORE == 21 || ...>
Though that's surely too verbose, but you can do this:
<#assign store = TEST_CONTACTS_LIST.PREFERRED_STORE>
<#if store == 12 || store == 21 || ...>
But I think what you are looking for is this (or this combined with the #assign, if you have several #elseif-s):
<#if [12, 21, ...]?seq_contains(TEST_CONTACTS_LIST.PREFERRED_STORE)>
This is a possibility too (just don't forget the #break-s):
<#switch TEST_CONTACTS_LIST.PREFERRED_STORE>
<#case 12><#case 21>...
{bayarea}
<#break>
<#case 34><#case 62>...
{bluebridge}
<#break>
...
</#switch>
Those are four good answers above. To expand on it, I'd do something like this:
<#assign pref_store = TEST_CONTACTS_LIST.PREFERRED_STORE>
<#assign area = "">
<#assign group1 = [12,21,22,38,46,67,71,74,76,77,83,86,104,113,119,143]>
<#assign group2 = [34,62,84,91,137,144,152,169]>
<#assign group3 = [18,44,49,50,61,68,121,182]>
<#if group1?seq_contains(pref_store)>
<#assign area = "bayarea">
<#elseif group2?seq_contains(pref_store)>
<#assign area = "blueridge">
<#elseif group3?seq_contains(pref_store)>
<#assign area = "frontrange">
<#else>
<#assign area = "whatever">
</#if>
Your Preferred Store Area is ${area}.
Alternatively, instead of Sequences / Arrays, you could also set these values as a string.
For example:
<#assign pref_store = "${TEST_CONTACTS_LIST.PREFERRED_STORE}">
<#assign group1 = "12,21,22,38,46,67,71,74,76,77,83,86,104,113,119,143">
then the if statement would be the following:
<#if group1?contains(pref_store)>
(Notice the ?contains instead of ?seq_contains)
etc..

How to convert from currency apply addition then use save currency for new sum?

How can I add the following two strings that represent currency (these could be different than en_us currency)?
<#assign op1 = '$5.50'>
<#assign op2 = '$1.00'>
<#assign sum = op1 + op2>
where sum prints out: '$6.50'
Maybe I don't understand the question, but how about:
<#-- Calculate the sum: -->
<#assign op1 = 5.50>
<#assign op2 = 1.00>
<#assign sum = op1 + op2>
...
<#-- Later print out the sum: -->
$${sum?string('0.00')}
Or if you want to build on Java's currency formatter:
${sum?string.currency}
BTW, a template that calculates such business data stinks... it's not the duty of the template. The template is to deal with the formatting/visual-design aspects.

Assigning a substitute value in case of nil

I have a few variables:
name
age
address
phone
social_security
email
weight
And an array called personal_details with each of these values in positions 0-6.
So I assign values like this:
name = personal_details[0]
address = personal_details[1]
phone = personal_details[2]
social_security = personal_details[3]
email = personal_details[4]
weight = personal_details[5]
In some cases, however, the data on the right hand side does not exist.
What's a more elegant way to handle this than writing something like this for each element in the array?
if !personal_detail[0].nil?
name = personal_details[0]
else
name = ""
end
if !personal_detail[1].nil?
address = personal_details[1]
else
address = ""
end
You could do this since a nil value returns false and || will only evaluate the right-hand side if the left-hand side is false:
name = personal_details[0] || ''
name = personal_details[0] || ""
Update: This is incorrect, see discussion below.
There is a built-in way to do this with Array#fetch:
personal_details = ['Joe User', nil, '12 Main Street']
name = personal_details.fetch(0, '')
age = personal_details.fetch(1, '')
address = personal_details.fetch(2, '')
The other solutions will work fine based on the example you provided. If one of the values on the right-hand side is set to false, the || approach will return the empty string instead of the value.

Resources