Search code examples
freemarker

How can I convert a List to an Array in FreeMarker?


I have this Array (SimpleSequence):

<#assign arrayList = [100, 200, 300, 400, 500] />

<#list arrayList as item>
    <#assign addToArray = "<p>" + item + "</p>" />
</#list>

I want to add the addToArray to a new Array.

How can I accomplish this?


Solution

  • According to the comments you want to transform each item of a sequence to create another sequence (list or array... FreeMarker doesn't care). Unfortunately there's no map function in FTL (as of 2.3.25), but as far as the sequence is not very long (as then this becomes slow), you can work that around with sequence concatenation:

    <#assign array = [100, 200, 300, 400, 500] />
    
    <#assign mappedArray = []>
    <#list array as item>
        <#assign mappedArray += ["<p>${item}</p>"]>
    </#list>
    

    (In case Liferay uses a too old version, you may have to write <#assign mappedArray = mappedArray + ["<p>${item}</p>"]> instead.)