Package org.openxava.util
Class SimpleTemplater
java.lang.Object
org.openxava.util.SimpleTemplater
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuildOutputUsingInputStreamTemplate(InputStream templateIS, Map<String, Object> parameters) Returns the template with all fields replaced by their valuesbuildOutputUsingResourceTemplate(String templateName, Map<String, Object> parameters) Returns the template with all fields replaced by their valuesbuildOutputUsingStringTemplate(String template, Map<String, Object> parameters) Returns the template with all fields replaced by their valuesstatic SimpleTemplaterReturns an instance of SimpleTemplater which will not deal with HTMLstatic SimpleTemplatergetInstance(boolean templateIsHTML) Returns an instance of SimpleTemplater which will or will not deal with HTMLstatic 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>booleantemplateNameIsHTML(String templateName)
-
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
Returns an instance of SimpleTemplater which will not deal with HTML- Returns:
- SimpleTemplater
-
getInstance
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
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 parameterfrom- - start of the sequenceto- - end of the sequence- Returns:
- a list of Map<String, Object>
-
buildOutputUsingResourceTemplate
Returns the template with all fields replaced by their values- Parameters:
templateName- - the name of the template to be found in the code archiveparameters- - 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 InputStreamparameters- - 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
Returns the template with all fields replaced by their values- Parameters:
template- - a String containing the templateparameters- - 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
-