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 String length built-in function.
//Get template from classpath
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(StringLengthTest.class, "/");
Template template = cfg.getTemplate("length.ftl");
//Prepare data model
Map<String, Object> dataModel = new HashMap<String, Object>();
dataModel.put("msg", "be the coder");
dataModel.put("msg2", "ABCD");
//Merge template and data
OutputStreamWriter output = new OutputStreamWriter(System.out);
template.process(dataModel, output);
}
}
It gives the following output,
Length of [be the coder] : 12
Length of [ABCD] : 4
Length of [abcbabdaaabcaadaabd] : 19