BB-8
BB-8

Reputation: 625

Freemarker dynamically adding elements into array

I am trying to list all my tag names in one column. While listing them I am storing each tag's name into array tagarray. Below the list, I want to output whole array inside <p>.

        <div>
            <#assign tagarray = []>
            <#list elements as element>
                <#if element.tags??>
                    <#list element.tags as tag>
                        <#if tag.myProperty??>
                            <span class="section-name">${tag.text}</span>
                            <br />
                            <#assign tagarray = tagarray + ["${tag.text}"]>
                        </#if>
                    </#list>
                </#if>
            </#list>
            <p>${tagarray}</p>
        </div>

The issue is, this line of code:

<#assign tagarray = tagarray + ["${tag.text}"]>

doesn't seem to work. According to some websites the syntax above is correct, so I don't know why it is not working.

Upvotes: 1

Views: 5185

Answers (2)

Prasad Perabathula
Prasad Perabathula

Reputation: 23

Here code is there but you can't modify line to array value before executing the step 2 how many values is there that values is only process in step 2 in our case only one but in step 4 it will add 3 values then now array values are fore so step 5 execute 4 times. step 2 only execute only one time.

1. <#assign array=[0]>
2. <#list array as a>
3.    ${a}
4.      <#assign array = array + [1]><#assign array = array + [1]><#assign array = array + [1]>
5.      <#list array as c>
6.          ${c}
7.      </#list>
8. </#list>

Upvotes: 0

ddekany
ddekany

Reputation: 31152

The assignment line looks syntactically correct (though you don't need the $ in it, you can just write ... + [tag.text], also you can make it less verbose with +=, like <#assign tagarray += [tag.text]>). ${tagarray} will fail as it's not clear how to format the sequence. Maybe you want ${tagarray?join(', ')} there.

Also note that if add sequences for many many times, listing the result will be slow. It's might not be a concern in your use case though.

Upvotes: 2

Related Questions