Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freemarker assign list length to local variable

Tags:

The following freemarker code causes an exception

<#assign i= it.getList().size()> <#list it.getList() as elem>     <#if i==1>     <li>${elem.name}</li>     <#else>     <li class="marked">${elem.name}</li>     </#if>     <#assign i = i-1> </#list> 

The following exception is thrown:

Expected hash. it.getList() evaluated instead to freemarker.template.SimpleSequence

Anyone knows why? How can i assign the length of the list to my variable i?

like image 498
cuh Avatar asked Oct 05 '10 13:10

cuh


People also ask

How do I find the size of a list in FTL?

To get the size of your list you must use list? size , that means boxen. getSiblings()? size for this example.

How do you create a list in FreeMarker?

FreeMarker doesn't support modifying collections. But if you really want to do this in FreeMarker (as opposed to in Java), you can use sequence concatenation: <#assign myList = myList + [newItem]> . Here you create a new sequence that wraps the two other sequences.

How do you append strings in FreeMarker?

$( document ). ready(function() { var html = ${itemsToAppendTo} $('. gTA'). append( html ) });


1 Answers

I figured out, that it did not understood the syntax for the size built-in. The right syntax for assigning the size of a list to a local variable is

<#assign i = it.getList()?size> 
like image 84
cuh Avatar answered Oct 01 '22 14:10

cuh