Class TemplateProcessor

java.lang.Object
com.renomad.minum.templating.TemplateProcessor

public final class TemplateProcessor extends Object
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 Details

    • renderTemplate

      public String renderTemplate(Map<String,String> myMap)
      Given a map of key names -> value, render a template.
    • renderTemplate

      public String renderTemplate(List<Map<String,String>> myMap)
      Given a list of maps of key names -> value, render a template multiple times.
    • renderTemplate

      public String renderTemplate()
      Recursively assembles the template and sub-templates
    • renderTemplate

      public String renderTemplate(boolean runWithChecks)
      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

      public void registerData(List<Map<String,String>> dataList)
      Assign data. Keys must match to template.
    • buildProcessor

      public static TemplateProcessor buildProcessor(String template)
      Builds a TemplateProcessor from a string containing a proper template. Templated values are surrounded by double-curly-braces, i.e. {{foo}} or {{ foo }}
    • registerInnerTemplate

      public TemplateProcessor registerInnerTemplate(String key, TemplateProcessor innerTemplate)
      Binds an inner template to a key of this template.
    • getOriginalText

      public String getOriginalText()
      Returns the original unchanged template string
    • getInnerTemplate

      public TemplateProcessor getInnerTemplate(String innerTemplateKey)
      Returns the reference to an inner template, to enable registering data and sub-templates.