cancel
Showing results for 
Search instead for 
Did you mean: 

Hint of the day #2 : Creating a Map or Seq. with Freemarker

zomurn
Champ in-the-making
Champ in-the-making
This example enumerate all the associations of the root node "person".
Each association value is a nodeRef to a node of type cmSmiley Tongueerson
Hence, this freemarker script list the usernames associated with the current person : allow to represent a human hierarchy inside a company.

In two steps :

1) Create the Map "as string"
2) Cast the string to a Map object with the instruction :

<#assign map = mapAsString?eval/>

3) Test for NON existence of a key in the map :

<#if !map["keynotexist"]??>


<#assign mapAsString = "{"/>
<#assign first = true />
<#list person.assocs["cm:subscribedBy"] as subscriber>
    <#if first = false>
        <#assign mapAsString = mapAsString + ","/>
    </#if>
    <#assign mapAsString = mapAsString + "\"" + subscriber.properties["userName"] + "\":" + "\"" + subscriber.properties["userName"] + "\""/>
    <#assign first = false/>
</#list>
<#assign mapAsString = mapAsString + "}"/>

<#assign map = mapAsString?eval/>
<p><i>map size: ${map?size}</i></p>
<#assign keys = map?keys>
<#list keys as key>
   ${key} = ${map[key]}
</#list>
<#if !map["keynotexist"]??>
   <p><i>key not found</i></p>
<#else>
   <p><i>key found</i></p>
</#if>

If you want a sequence, just build the String. For example "[value1,value2,…]" and then call "eval" like above.
And then do traverse like in the freemarker doc reference :  http://www.freemarker.org/docs/ref_builtins_hash.html

PS : according to the freemarker doc reference order may be maintained in the map. So I'am quite suspicious that behind this is a real java map…So it means that Map and Sequence in Freemarker looks to be the same java underlying data structure : a List, because they maintained the order both.

Hoping it might help someone (this hints in missing in the wiki)
4 REPLIES 4

mrogers
Star Contributor
Star Contributor
Please go ahead add your examples and hints to the wiki if you realise something is missing.

lyamamot
Champ in-the-making
Champ in-the-making
PS : according to the freemarker doc reference order may be maintained in the map. So I'am quite suspicious that behind this is a real java map…So it means that Map and Sequence in Freemarker looks to be the same java underlying data structure : a List, because they maintained the order both.

Thanks for the cool tip on how to form a map from a string!

I just wanted to point out that in Java, a LinkedHashMap maintains insertion order of the keys so the underlying structures may be different.

zomurn
Champ in-the-making
Champ in-the-making
As I've just seen, you might be right. That's true.

mrogers > I will put this hint on the wiki later so. But everyone can post on it ?

mrogers
Star Contributor
Star Contributor
Yes, all community members can update the wiki and I'd encourage everyone to do so as and when they see something missing or not quite right.