Freemarker get element from list

mkuligowski picture mkuligowski · Feb 19, 2014 · Viewed 14.4k times · Source

I am doing teplate which i will use in Smooks conversion. I need to get f.e. 2nd element of the list in freemarker but i have no idea how to do it. There is fragment of my code in freemarker template.

<#list partnerList as PARTNER>
    ${PARTNER.partnrid}
    ${PARTNER.name}
</#list>

and there is java class:

public class Partner {
    private String PARTNRID;
    private String NAME;

  public String getPartnrid() {
  return PARTNRID;
 }
 public void setPARTNRID(String PARTNRID) {
  this.PARTNRID = PARTNRID;
 }
 public String getName() {
  return NAME;
 }
 public void setNAME(String NAME) {
  this.NAME = NAME;
 }
}

As i said before i need ONLY 2nd element. I want to avoid printing the rest items. Thanks!

Answer

obourgain picture obourgain · Feb 19, 2014

You can get the second element of the list like this:

${partnerList[1].name}

See the freemarker documentation here.