Search code examples
javafreemarkerline-breakscarriage-return

How do I ignore a line break/carrige return with embedded list in freemarker?


I have a embedded list issue that I am trying to solve. The first list holds objects that may have list of objects. If the parent object has the list populated I would like to iterate over the objects and display the value. For some reason it keeps putting the output of the second list on a separate line. I thought from the documentation that it should ignore the line break from a LIST or IF directives. Below is a sample and the output I get.

<#list outsideRecordList as outsideRecord>
   ${(outsideRecord.fieldOne!"")?right_pad(9)}   ${(outsideRecord.fieldTwo)?trim} ${(outsideRecord.fieldThree)?trim} 
   <#if (outsideRecord.embeddedList)?has_content>
       <#list outsideRecord.embeddedList as innerRecord>
            <#if innerRecord.status == 'X'>
                <#lt>${""?right_pad(30)}${(innerRecord.fieldFour!"")}   
            </#if>  
      </#list>
 </#if>

What I get:

fieldOne      fieldTwo fieldThree
                                      fieldFour
                                      fieldFour 
                                      fieldFour
fieldOne fieldTwo fieldThree fieldOne fieldTwo fieldThree

I would like first fieldFour to be on the same line as the other fields and then any other fields to be on next line indented at the same posistion.

What I want: fieldOne fieldTwo fieldThree fieldFour fieldFour fieldFour fieldOne fieldTwo fieldThree fieldOne fieldTwo fieldThree


Solution

  • You have a line-break after both ${...}-s, and so there will be a line-break after "fieldThree". That's consistent with the documentation. You could <#rt> that line-break away, but then you had to decrease the indentation in the first iteration of the inner #list, also you had to ensure that if there's no "fieldFour" then the line-break is added back. Quite hard to follow. So, I think, the more template-ish approach is to print the first item of the embedded list just like you print "fieldThree" (inside an #if that checks if the embedded list has at least 1 item). So just like the first "fieldFour" is in the same line as "filedThree" in the output, its ${...} is in the same line in template too. Then, if the embedded list has ?size greater than 1, you could list the "fieldFour"-s starting from index 1, like <#list outsideRecord.embeddedList[1..] as innerRecord>.