Java - Handlebars

java-templates - Helpers

How can we use Handlebars with Java?

String doHipaaCheck = getConfigurationValue(pc,"HIPAA_CHECK");
String hipaaCheckDisplayEventID = getConfigurationValue(pc,"HIPAA_CHECK_DISPLAY_EVENT_ID");

String templateString = "...";
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline(templateString);
HashMap m = new HashMap();
m.put("eventID", eventID);
m.put("sql", reportSQLStatements_v);
m.put("timestamp", datetime);
m.put("hipaaCheckDisplayEventID", hipaaCheckDisplayEventID);

html = template.apply(m);

Example template:

<table style="vertical-align: top">
    <tr><td>Time stamp:</td><td>{{{timestamp}}}</td></tr>
        <tr><td>Event ID:</td><td>{{eventID}}</td></tr>
        <tr><td>SQL Statements:</td>
                {{#each sql}}

The above code assume that we can read the content of the template file into a string.

private String getTemplateContent(String fileName) {
    String result = "";
    try {
        result = FileUtils.readFileToString(new File(fileName), java.nio.charset.StandardCharsets.UTF_8.toString());
    } catch (Exception e) {
        // Ignore
    return result;

The code above use commons-io.jar from the Apache Commons project.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License