Search code examples
freemarkersmooks

Length of Assign variable issue in freemarker


I am using freemarker ..want to find the length of assign variable ..i used size and length function ..but it fails and returns the error ..Please help me in how to find length of the assign variable

Please find the below code i have tried...

Input data --- cusID="a-1242" -- I want to split input data by - and want to store in separate variable through assign function

 <#list (it.@CusID[0]!"")?split("-") as c><#if ((c?index) ==0)>
<#assign first>${c}</#assign>
<#assign firstlen = c?size>
</#if>
 </#list>

Above code firstlen is used to find the length but it fails to find length

ERROR MESSAGES find below

For "?size" left-hand operand: Expected an extended-hash or sequence or extended collection, but this has evaluated to a markup_output (wrapper: f.c.TemplateXMLOutputModel):


Solution

  • As the error message says, first stores XML markup, not just a plain text string. You can't get the length of markup with ?length, as it's not obvious what that means (like if the content of which XML elements matter, what if you have an entity reference, etc.). The reason it's markup is that <#assign first>...</#assign> is not a normal assignment, it's for capturing output, and you are using XML output format. Instead, use normal value assignment: <#assign first = c>. Now first will have the same type as c, string.