java.lang.Object
com.renomad.minum.templating.TemplateProcessor
This class provides methods for working with templates.
The first step is to write a template. Here is an example:
Hello, my name is {{name}}
Then, feed that string into the buildProcessor(java.lang.String)
method, like
this:
String input = "Hello, my name is {{name}}"
TemplateProcessor helloProcessor = TemplateProcessor.buildProcessor(input);
The returned value ("helloProcessor") can be rendered with different values. For example:
Map<String,String> myMap = Map.of("name", "Susanne");
String fullyRenderedString = helloProcessor.renderTemplate(myMap);
The result is:
Hello, my name is Susanne
-
Method Summary
Modifier and TypeMethodDescriptionstatic TemplateProcessor
buildProcessor
(String template) Builds aTemplateProcessor
from a string containing a proper template.getInnerTemplate
(String innerTemplateKey) Returns the reference to an inner template, to enable registering data and sub-templates.Returns the original unchanged template stringvoid
registerData
(List<Map<String, String>> dataList) Assign data.registerInnerTemplate
(String key, TemplateProcessor innerTemplate) Binds an inner template to a key of this template.Recursively assembles the template and sub-templatesrenderTemplate
(boolean runWithChecks) Render the template and any nested sub-templates.renderTemplate
(List<Map<String, String>> myMap) Given a list of maps of key names -> value, render a template multiple times.renderTemplate
(Map<String, String> myMap) Given a map of key names -> value, render a template.
-
Method Details
-
renderTemplate
Given a map of key names -> value, render a template. -
renderTemplate
Given a list of maps of key names -> value, render a template multiple times. -
renderTemplate
Recursively assembles the template and sub-templates -
renderTemplate
Render the template and any nested sub-templates. All templates must have data registered before running this method.- Parameters:
runWithChecks
- Default: true. Check that there is a 1-to-1 correspondence between the keys provided and keys in the template and sub-templates, throwing an exception if there are any errors. Also check that the maps of data are consistent. This should be set true unless there is a reason to aim for maximum performance, which is actually not valuable in most cases, since the bottleneck is the business algorithms, database, and HTTP processing.
-
registerData
Assign data. Keys must match to template. -
buildProcessor
Builds aTemplateProcessor
from a string containing a proper template. Templated values are surrounded by double-curly-braces, i.e. {{foo}} or {{ foo }} -
registerInnerTemplate
Binds an inner template to a key of this template. -
getOriginalText
Returns the original unchanged template string -
getInnerTemplate
Returns the reference to an inner template, to enable registering data and sub-templates.
-