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