public class Templator extends Object
Hello, {{name}}!Usage:
Templator templator = new Templator("/hello_template.txt"); System.out.println(templator.merge(map("name", "Kyla"))); // prints: Hello, Kyla!Note: Yes, I'm aware of existence of Mustache, Freemarker, Velocity, etc. This implementation is for quickly loading small snippets of text merged with data. The goal is to eliminate code pollution. If you need more power, use bigger frameworks.
Modifier and Type | Method and Description |
---|---|
String |
merge(Map<String,?> values)
This method is used in repeated operations, since it will load a resource once.
|
static String |
mergeFromPath(String templatePath,
Map<String,?> values)
This method is used in one-off operations, where it is OK to load a template every time.
|
static String |
mergeFromTemplate(String template,
Map<String,?> values)
Merges from string as template.
|
public Templator(String templatePath)
templatePath
- path to a template on classpathpublic String merge(Map<String,?> values)
values
- values to merge into a templatepublic static String mergeFromPath(String templatePath, Map<String,?> values)
String result = Templator.mergeFromPath(readResource("/message_template.txt", valuesMap));
templatePath
- template to mergevalues
- values to merge into a templateCopyright © 2019 JavaLite. All rights reserved.