TemplateUtils
Utility class with static helper methods for processing FreeMarker templates and performing simple variable substitution. Templates are loaded from the OpenKM home directory (Config.HOME_DIR). All methods are static.
FreeMarker configuration
getConfig
Description:
| Method | Return values | Description |
|---|---|---|
|
getConfig() |
Configuration |
Returns the singleton FreeMarker |
Template existence check
templateExists
Description:
| Method | Return values | Description |
|---|---|---|
|
templateExists(String name) |
boolean |
Returns |
|
name: The file name of the template relative to |
||
Example:
package com.openkm;
import com.openkm.util.TemplateUtils;
public class Test {
public static void main(String[] args) {
try {
System.out.println("Template exists: " + TemplateUtils.templateExists("OpenKM - NOTIFICATION"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Template variable replacement
replace (FreeMarker, returns String)
Description:
| Method | Return values | Description |
|---|---|---|
|
replace(String name, String template, Map<String, Object> model) |
String |
Processes a FreeMarker template string using the given model and returns the rendered result. |
|
name: A label used to identify the template in error messages. template: The FreeMarker template content as a string. model: A map of variable names to values for substitution. |
||
replace (FreeMarker, streams)
Description:
| Method | Return values | Description |
|---|---|---|
|
replace(String name, InputStream input, Map<String, Object> model, OutputStream out) |
void |
Reads a FreeMarker template from |
|
name: A label used to identify the template in error messages. input: An model: A map of variable names to values for substitution. out: The |
||
replace (simple string substitution)
Description:
| Method | Return values | Description |
|---|---|---|
|
replace(String template, Map<String, String> model) |
String |
Replaces all occurrences of |
|
template: A string containing model: A map of key names to replacement values. |
||
Example:
package com.openkm;
import com.openkm.core.Config;
import com.openkm.util.TemplateUtils;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
try {
// FreeMarker replace
Map<String, Object> model = new HashMap<>();
model.put("fileIn", "/tmp/input.xls");
String cmd = TemplateUtils.replace("SYSTEM_XLS2CSV", Config.SYSTEM_CATDOC_XLS2CSV, model);
System.out.println("Command: " + cmd);
// Simple string substitution
Map<String, String> vars = new HashMap<>();
vars.put("user", "jsmith");
vars.put("doc", "report.pdf");
String msg = TemplateUtils.replace("Hello ${user}, your document ${doc} is ready.", vars);
System.out.println(msg); // "Hello jsmith, your document report.pdf is ready."
} catch (Exception e) {
e.printStackTrace();
}
}
}
Variable helpers
toVar
Description:
| Method | Return values | Description |
|---|---|---|
|
toVar(String id) |
String |
Wraps the given identifier in |
Built-in templates
useMailTemplate
Description:
| Method | Return values | Description |
|---|---|---|
|
useMailTemplate(String body) |
String |
Wraps the given HTML body in the built-in OpenKM mail template ( |
|
body: The HTML content to embed in the mail template. |
||
useNotesTemplate
Description:
| Method | Return values | Description |
|---|---|---|
|
useNotesTemplate(String docName, List<NodeNote> notes) |
String |
Renders the built-in OpenKM notes template ( |
|
docName: The name of the document whose notes are being rendered. notes: A list of |
||
Example:
package com.openkm;
import com.openkm.util.TemplateUtils;
import com.openkm.util.TemplateUtils.NodeNote;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
try {
// Wrap HTML content in the mail template
String html = TemplateUtils.useMailTemplate("<p>Your document has been approved.</p>");
System.out.println(html);
// Render notes
List<NodeNote> notes = new ArrayList<>();
NodeNote n = new NodeNote();
n.setAuthor("jsmith");
n.setDate("2024-03-15");
n.setText("Reviewed and approved.");
notes.add(n);
String notesHtml = TemplateUtils.useNotesTemplate("report.pdf", notes);
System.out.println(notesHtml);
} catch (Exception e) {
e.printStackTrace();
}
}
}