Iterating over map in Freemarker [duplicate] - freemarker

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Freemarker iterating over hashmap keys
I have a Hash Map which contain item ids as a key and Item objects as a value. Following is the pseudo code -
allItems : {
12: itemObj1 (id:12, name:myitem1)
13: itemObj2 (id:13, name:myitem2)
14: itemObj3 (id:14, name:myitem3)
}
On result.ftl I need to iterate over this map and get the values of Item Object. I have tried this approach but could not get values from Item object -
<#list item?keys as it>
${it} = ${item.get(it)[name]}
</#list>

I think you want:
<#list allItems?keys as it>
${it} = ${allItems[it].name}
</#list>

<#assign seq=["a","b","c"]>
<#list seq as l>
${l[1]}
// It will print b
${l[0]}
//It will print a
</#list>

Related

Trying to generate a string that becomes a variable name in Freemarker or a "derived variable name"

I'm using a data-directive or list to pull in several values, of which, I don't know how many there will be, and I want to try and list through them, and create several variables in the list if you will.
<#list 1..10 as x>
<#-- the next line doesn't work, but what i'm trying to fix -->
<#assign .vars['VAR'+x?string] = rand(100) />
</#list>
But I can list them back out that way.
<#list 1..10 as x>
${.vars['VAR'+x?string]}
</#list>
The documentation for assign, says:
name: name of the variable. It is not expression. However, it can be
written as a string literal, which is useful if the variable name
contains reserved characters, for example <#assign "foo-bar" = 1>.
Note that this string literal does not expand interpolations (as
"${foo}").
Is there no way around this? Am I trying to do the impossible? Is there some way I can insert a derived name into the .vars... Hash is it?
A little more research that was close, but didn't get me there:
This prevoius question gives how to READ the derived variable, but I need to WRITE/CREATE the derived variable.
FreeMarker get variable value by concatenating another variable value
This prevoius question shows that I can use a string to assign a variable and re-iterates what we saw in the first link.
Variable name in Freemarker Template Language
As FreeMarker can't assign to elements of collections (but you can ?map(it -> ...) a collection to another), the only way is via ?interpret:
<#list 1..10 as x>
<#-- the next line doesn't work, but what i'm trying to fix -->
<#'<#assign VAR${x?c} = rand(100)>'?interpret />
</#list>
I wonder why do you need to assign to a dynamically named variables though, since reading dynamically named variables is also a pain, even if a lesser one.
Ultimately, I believe the correct way to phrase my solution was a sequence of hashes:
<#list 1..z_Coupon_Pet.NUM_COUPONS as x>
<#assign INSTORE_COUPON=call_coupon_from_table1() />
<#assign ONLINE_COUPON=call_coupon_from_table2() />
<#assign coupon_string_row= '{
"COUPON_NUM" : ${x},
"INSTORE" : "${INSTORE_COUPON?js_string}",
"ONLINE" : "${ONLINE_COUPON?js_string}"
}' />
<#if x==1>
<#assign coupon_hash_string = coupon_string_row />
<#else>
<#assign coupon_hash_string = coupon_hash_string + ',' + coupon_string_row />
</#if>
</#list>
</#if>
<#if coupon_hash_string?has_content>
<#assign coupon_hash=parsejson('[' + coupon_hash_string + ']') />
</#if>
We specifically avoid <#assign my_hash = my_hash + element /> because of this note in the documentation:
Note that hash concatenation is not to be used for many repeated concatenations, like for adding items to a hash inside a loop. While adding together hashes is fast and is constant time (independent of the size of the hashes added), the resulting hash is a bit slower to read than the hashes added together. Thus after tens... of additions the result can be impractically slow to read.

Freemarker: convert sting to list with split then iterate

I need to iterate through a list made from the "split" built-in yet I haven't succeeded.
I have a two lists of dates that share some of the same dates. using "seq_index_of" as a "Vlookup" i'm able to find the correlation and get the index of the dates that are in both lists. however, "seq_index_of" gives a string, number, boolean or date/time values output and not a sequence (for me to iterate).
I use "split" to turn the string into a sequence. split wont truly do the job, though.
first i use seq_index_of for the two lists:
this will give me the index of correlated dates.
<#assign result>
<#list list1 as L1>
${list2?seq_index_of(L1)}
</#list>
</#assign>
---------------------------------------
next I iterate the result. for this, imagine "array" is the result from above:
ex 1. this does not work. cant iterate
<#assign array = "2020-10-02,2021-10-04,2022-10-04,2023-10-04" />
<#assign mappedArray_string = []>
<#list array?split(",") as item>
<#assign mappedArray_string += [item]>
</#list>
${"is_sequence: " + mappedArray_string?is_sequence?c}
<#--print the list-->
<#list mappedArray_string as m>
<#--i cant do ?string("MM/dd/yyyy") or ant other iteration
${m?string("MM/dd/yyyy")}
<#if m?has_next>${";"}<#else>${"and"}</#if>-->
<#with no iteration, this will work. not what i need-->
${m}
</#list>
+++++++++++++++++++++++++++++++++++++++++++++
ex 2. - this works with a regular numerical list
<#assign array = [100, 200, 300, 400, 500] />
<#assign mappedArray_numbers = []>
<#list array as item>
<#assign mappedArray_numbers += [item]>
</#list>
${"is_sequence: " + mappedArray_numbers?is_sequence?c}
<#--print the list-->
<#list mappedArray_numbers as m>
${m?string("##0.0")}<#-- able to iterate-->
</#list>```
expected:
date1 ; date2 ; date3 and lastdate
error:
For "...(...)" callee: Expected a method, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> m?string [in template "preview-template" at line 27, column 3]
----
FTL stack trace ("~" means nesting-related):
 - Failed at: ${m?string("MM/dd/yyyy")}
If you want to list the items of list2 that also occur in list1, then do this (requires FreeMarker 2.3.29 because of ?filter):
<#list list2?filter(it -> list1?seq_contains(it)) as d>
${d?string('MM/dd/yyyy')}
</#list>
Before 2.3.29:
<#list list2 as d>
<#if list1?seq_contains(d)>
${d?string('MM/dd/yyyy')}
</#if>
</#list>

FreeMarker template - Double sorting

I am working on FreeMarker template for creating chart outputs using HighChart and we have a requirement to do double sorting based on 2 different columns.
Example: Sorting country list using 2 different columns "Region" and "Country"
I checked other forums and found double sorting has been defined within array elements as specified below but not at an individual element level.
current.children?sort_by('type')?sort_by(['properties','cm:name'])
Here is the code snippet
<#--Assign the array values-->
<#assign country = country + [ {
"category":"${level1}",
"value":level1Value?number,
"color": color?string,
"superRegion":"${level3?upper_case}",
"region":"${level2?upper_case}"
} ] >
Sorting the list - The below double sorting doesn't work. Does anyone know how to do this double sorting ?
<#list country?sort_by("superRegion")?reverse?sort_by("region") as countrySorted>
?sort_by can't sort by a composite "key" (as of 2.3.23). Applying ?sort_by twice just re-sorts the whole list.
Usually, lists should be sorted before passing them to the template anyway. I'm not sure if that's feasible in your case. If not, you can still write a TemplateMethodModelEx for it and put that into the data-model etc. (Plus I have made a note that ?sort_by should be able to do this, since it already exists anyway... maybe some contributor will pick it up.)
Resolved case like this. Using sort_by function twicely won't help you. I resolved this way: list, filter, sort_by. Here example.
Can be checked here: https://try.freemarker.apache.org/
Template:
Sorted by gender:
<#list humans?sort_by("gender") as h>
<#if !(genders??)>
<#assign prevGender = h.gender />
<#assign genders = h.gender + "---" />
</#if>
<#if prevGender != h.gender>
<#assign genders = genders + h.gender + "---" />
</#if>
<#assign prevGender = h.gender />
index: ${h.index}, gender: ${h.gender}, name: ${h.name}, age: ${h.age}.
</#list>
Sorted by gender, name:
<#list genders?split("---") as g>
<#if g?? && g?trim!="">gender: ${g}</#if>
<#list humans?filter(x -> x.gender = g)?sort_by("name") as x>
index: ${x.index}, gender: ${x.gender}, name: ${x.name}, age: ${x.age}.
</#list>
</#list>
Data model:
humans=[{"index":0,"gender":"male","name":"Bazil","age":34},{"index":1,"gender":"male","name":"Serhio","age":33},{"index":2,"gender":"male","name":"Andrew","age":32},{"index":3,"gender":"male","name":"Arty","age":34},{"index":4,"gender":"male","name":"Alex","age":33},{"index":5,"gender":"female","name":"July","age":30},{"index":6,"gender":"female","name":"Anna","age":31},{"index":7,"gender":"female","name":"Tatiana","age":32}]
You can overwrite your list:
<#assign mylist = mylist?sort_by("ID") />
<#assign mylist = mylist?sort_by("TYPETRAITEMENT") />
<#assign mylist = mylist?sort_by("DOMAINEMETIER") />

Freemarker - How to put variables in hashmap?

I searched the web to find how to add some entries into an existing hashmap.
Consider this code:
<#assign foo={'bar':'go'}>
I want to add a new entry and have something like this:
foo={'bar':'go','new':'entry}
How can I do that?
Using concatenation:
<#assign foo=foo+{'new':'entry'}>
print the hashmap:
<#list foo?keys as k>
${k}: ${foo[k]} <br>
</#list>
The result is exactly what you want:
bar: go
new: entry
D.

Freemarker assign value to variable

I'm having trouble while trying to assign value to a variable using Freemarker.
<#if size??>
<#assign num=size?number>
<#if (num>0)>
<#list 0..num-1 as i>
<#if .vars['abc'+i?c] = "test">
<#assign .vars['abc'+i?c] = .vars['abc'+i?c]?replace("test","Test")>
</#if>
</#list>
</#if>
This is the error message: Encountered ".", but was expecting one of:
STRING_LITERAL
RAW_STRING
ID
Can anyone help me with this?
Thank you.
You can only write top-level variables in a FreeMarker template. Also you can't assign to a variable with dynamically constructed name, except with an ?interpret hack: <#"<#assign abc${i?c} = abc${i?c}?reaplce('test', "Test")>"?interpret />. Obviously that's horrid... BTW, what's the use case here? Why do you need to assign to dynamically constructed variable names?

Resources