Java - Handlebars

java-templates

https://jknack.github.io/handlebars.java/gettingStarted.html - Helpers
https://github.com/jknack/handlebars.java

How can we use Handlebars with Java?

reportSQLStatements_v.add("A");
reportSQLStatements_v.add("B");
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>
    {{#hipaaCheckDisplayEventID}}
        <tr><td>Event ID:</td><td>{{eventID}}</td></tr>
        <tr><td>SQL Statements:</td>
            <td>
                {{#each sql}}
                    {{this}}<br/>
                {{/each}}
            </td>
        </tr>
    {{/hipaaCheckDisplayEventID}}
</table>

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