How to Write Series in FreeMarker? - 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>

Related

Print only 5 line of each having 32 chars using Freemarker

How to print only 5 line of each having 32 chars using Freemarker. Currently i have the below solution. Is there any better way of doing using split or substring
<#assign msg="Tell FreeMarker to convert string to real date-time value Convert date-time value to date-only value Let FreeMarker format it according the date_format setting">
<#assign len=msg?length>
<#list 1..5 as i>
<#assign start=(i-1)*32>
<#assign end=i*32>
<#if (end <len)>
${msg[start..end]}
<#else>
${msg[start..len-1]}
</#if>
</#list>
result is
Tell FreeMarker to convert string
g to real date-time value Convert
t date-time value to date-only va
alue Let FreeMarker format it acc
cording the date_format setting
Like this:
<#list msg?matches(".{1,32}")[0..*5] as row>
${row}
</#list>
Note that the "length limited range" operator, ..*, doesn't give error if the length is less than what you asked for. So even with your approach, you can remove the end assignment and the #if/#else, and just use ${msg[start..*32]}.

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

Repeat a string n times in freemarker

I'm looking for a concise way to repeat a string of characters n times, where n is a variable. I couldn't find good wat to do that in the docs.
You could simply use list to iterate a range:
<#assign n = 5>
<#list 0..<n as i>hello</#list>
Or as a macro:
<#macro repeat input times>
<#list 0..<times as i>${input}</#list>
</#macro>
<#repeat input="hello" times=5/>
If you only need to repeat a single character c for n times, you could do ${''?left_pad(n, c)}. It's a bit critic though, so perhaps you want to put it into a #function with proper name.

Loop with odd sequence

How to write loop with odd sequence in Apache FreeMarker template?
for example:
<#list seq as n>
...?
${n_index}
</#list>
As result: 1,3,4,5..
Use the Modulus operator.
<#list seq as n>
<#if n % 2 == 1>
<#-- your code here -->
</#if>
</#list>
Assuming you actually want to print the 1st, 3rd, 5th, etc. item of the sequence, as opposed to filter by the parity of the list item (n) itself...
If the result is 1, 2, etc., then either you actually want the even items, or you want n?counter that is 1-based, not n?index that is 0-based. Assuming the last (plus I also print the item itself):
<#list seq as n>
<#if n?is_odd_item>
${n?counter}: ${n}
</#if>
</#list>
Related page in the manual: http://freemarker.org/docs/ref_builtins_loop_var.html

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.

Resources