Another option would be to create your own Freemarker method and introduce it into the data model.
Here is an example of how to do this in Java 8:
public String generate(Map<String, Object> data, String templateLocation) throws IOException, TemplateException {
try (StringWriter writer = new StringWriter()) {
Template template = configuration.getTemplate(templateLocation);
data.put("uuid", (TemplateMethodModelEx) (list) -> UUID.randomUUID());
Random r = new Random();
// Adding method for generation of random number
data.put("randomNumber", (TemplateMethodModelEx) (list) -> r.nextInt(200));
template.process(data, writer);
return writer.toString();
}
}
In the Freemarker template, you can use this method using:
${randomNumber()}
source
share