Class SimpleTemplater

java.lang.Object
org.openxava.util.SimpleTemplater

public class SimpleTemplater extends Object
To process simple HTML templates. -- Usage -- SimpleTemplater.getInstance().buildOutputUsingStringTemplate(template, parameters); Replaces ${parameter_name} in the template by its value found in the (Map<String, Object>)parameters. -- Syntax -- Simple blocks can be processed. Blocks are surrounded by and $$for(parameter_name) will loop over the items of parameter_name of type Collection<Map<String, Object>> $$if(parameter_name) will execute only if parameter_name is defined and not empty $$ifnot(parameter_name) will execute only if parameter_name is not defined or is empty -- Example -- String template = " Dear ${name}, Here is the list of your things: ${name} Regards, Sincerely, Me"; Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("name", "You"); parameters.put("regards", "1"); Collection<Map<String, Object>> things = new ArrayList<Map<String, Object>>(); for (int i=0; i<3; i++) { Map<String, Object> p = new HashMap<String, Object>(); p.put("name", "Thing + i); things.addElement(p); } parameters.put("things", things); String output = SimpleTemplater.getInstance().buildOutputUsingStringTemplate(template, parameters); System.out.println(output);
Author:
Laurent Wibaux
  • Constructor Details

    • SimpleTemplater

      public SimpleTemplater(boolean templateIsHTML)
      Constructor
      Parameters:
      templateIsHTML - - set to true if the template that will be fed is HTML
  • Method Details

    • getInstance

      public static SimpleTemplater getInstance()
      Returns an instance of SimpleTemplater which will not deal with HTML
      Returns:
      SimpleTemplater
    • getInstance

      public static SimpleTemplater getInstance(boolean templateIsHTML)
      Returns an instance of SimpleTemplater which will or will not deal with HTML
      Parameters:
      templateIsHTML - - set to true if the template that will be fed is HTML
      Returns:
      SimpleTemplater
    • getSequence

      public static Collection<Map<String,Object>> getSequence(String indexName, int from, int to)
      Returns a sequence of numbers going from 'from' to 'to', using indexName as key in the map this sequence can then be added to a Map<String, Object>
      Parameters:
      indexName - - the name of the parameter
      from - - start of the sequence
      to - - end of the sequence
      Returns:
      a list of Map<String, Object>
    • buildOutputUsingResourceTemplate

      public String buildOutputUsingResourceTemplate(String templateName, Map<String,Object> parameters)
      Returns the template with all fields replaced by their values
      Parameters:
      templateName - - the name of the template to be found in the code archive
      parameters - - Map<String, Object> of the parameters
      Returns:
      the result of the processing
      Throws:
      SimpleTemplaterException - - in case of an error in the template or in the passed parameters
    • buildOutputUsingInputStreamTemplate

      public String buildOutputUsingInputStreamTemplate(InputStream templateIS, Map<String,Object> parameters)
      Returns the template with all fields replaced by their values
      Parameters:
      templateIS - - an InputStream
      parameters - - Map<String, Object> of the parameters
      Returns:
      the result of the processing
      Throws:
      SimpleTemplaterException - - in case of an error in the template or in the passed parameters
    • buildOutputUsingStringTemplate

      public String buildOutputUsingStringTemplate(String template, Map<String,Object> parameters)
      Returns the template with all fields replaced by their values
      Parameters:
      template - - a String containing the template
      parameters - - Map<String, Object> of the parameters
      Returns:
      the result of the processing
      Throws:
      SimpleTemplaterException - - in case of an error in the template or in the passed parameters
    • templateNameIsHTML

      public boolean templateNameIsHTML(String templateName)