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 iterating object lists.
//Get template from classpath
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(ObjectListTest.class, "/");
Template template = cfg.getTemplate("objlist.ftl");
//Prepare data model
Student std1 = new Student("Sriram", 2, "Chess");
Student std2 = new Student("Sudhakar", 29, "Painting");
Student std3 = new Student("Anu", 28, "Cooking");
List<Student> students = Arrays.asList(std1, std2, std3);
Map<String, Object> dataModel = new HashMap<String, Object>();
dataModel.put("students", students);
//Merge template and data
OutputStreamWriter output = new OutputStreamWriter(System.out);
template.process(dataModel, output);
}
}
It gives the following output,
Name : Sriram
Age : 2
Hobby : Chess
Name : Sudhakar
Age : 29
Hobby : Painting
Name : Anu
Age : 28
Hobby : Cooking