oldpilsbury
oldpilsbury

Reputation: 1

Conditionally display line in freemarker template

I'm creating a PDF from a sorted array of JSON objects, sorted by task, where each object, e.g. JSON.arr[0] == i, contains memo, rate, amount and taskText.

I want to display the task on its own line only if the current task is the first task, or different from the previous task. Nothing is being displayed for taskText.

<#if JSON?has_content>
<table>
  <tr>
    <th colspan="5">MEMO</th>
    <th>RATE</th>
    <th>AMOUNT</th>
    </tr>
  <#list JSON.arr as i>
    <#if i?first>
      <#assign task = i.taskText>
      <tr><td>${i.taskText}</td></tr>
    </#if>
    <#if i.taskText != task>
      <#assign task = i.taskText>
      <tr><td>${i.taskText}</td></tr>
    </#if>
    <tr>
      <td colspan="5">${i.memo}</td>
      <td>$${i.rate}</td>
      <td>$${i.amount}</td>
    </tr>
  </#list>
</table>
</#if>

Upvotes: 0

Views: 836

Answers (1)

ggradnig
ggradnig

Reputation: 14169

Use the built-in ?is_first instead of ?first. It is used to check whether the current item is the first one in the current list.

For the other requirement, I would suggest to assign an initial value to task before the loop. It may well be that task is never set to anything other than null and comparison therefore fails.

Upvotes: 1

Related Questions