tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Template Engines > Freemarker > List Special Variables

List Special Variables 

FreeMarker is a java based template engine for complex template processing. This requires the library freemarker-2.3.16.jar to be in classpath. The following example shows using LOOPVAR_index and LOOPVAR_has_next special loop variables. The first one is a loop count index and other is a boolean specifying whether the current item is last in the sequence or not.

File Name  :  
/FREEMARKER001/config/list_index.ftl 

File Name  :  
com/bethecoder/tutorials/freemarker/tests/ListIndexTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.freemarker.tests;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class ListIndexTest {

  /**
   @param args
   @throws IOException 
   @throws TemplateException 
   */
  public static void main(String[] argsthrows IOException, TemplateException  {

    //Get template from classpath
    Configuration cfg = new Configuration();
    cfg.setClassForTemplateLoading(ListIndexTest.class, "/");
    Template template = cfg.getTemplate("list_index.ftl");
    
    //Prepare data model
    Map<String, Object> dataModel = new HashMap<String, Object>();
    
    //Merge template and data
    OutputStreamWriter output = new OutputStreamWriter(System.out);
    template.process(dataModel, output);
  }
}
   

It gives the following output,
1. ONE (has next)
2. TWO (has next)
3. THREE (has next)
4. FOUR 

1. 10,000 (has next)
2. 10,001 (has next)
3. 10,002 (has next)
4. 10,003 (has next)
5. 10,004 



 
  


  
bl  br