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 how to generate HTML for rendering a simple menu.
//Get template from classpath
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(MenuRenderTest.class, "/");
Template template = cfg.getTemplate("menu.ftl");
//Prepare data model
Category first = new Category("First Category");
first.getMenuItems().add(new MenuItem("One", "http://first.com/One"));
first.getMenuItems().add(new MenuItem("Two", "http://first.com/Two"));
Category second = new Category("Second Category");
second.getMenuItems().add(new MenuItem("One", "http://second.com/One"));
second.getMenuItems().add(new MenuItem("Two", "http://second.com/Two"));
second.getMenuItems().add(new MenuItem("Three", "http://second.com/Three"));
List<Category> categoryList = Arrays.asList(first, second);
Map<String, Object> dataModel = new HashMap<String, Object>();
dataModel.put("categoryList", categoryList);
//Merge template and data
OutputStreamWriter output = new OutputStreamWriter(System.out);
template.process(dataModel, output);
}
}