Java String Template Example

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.stringtemplate.v4.ST;

public class StringTemplateHelper {

        public static void main(String[] args) throws IOException,
                        URISyntaxException {
                // TODO Auto-generated method stub
                StringTemplateHelper stHelper = new StringTemplateHelper();
                ST emailTestTemplate = new ST(new String(Files.readAllBytes(Paths
                                .get(StringTemplateHelper.class.getResource(
                                                "/email/email-Test.html").toURI()))), '$', '$');
                System.out.println(stHelper.processTemplate(emailTestTemplate));

        }

        public String processTemplate(ST emailTestTemplate) throws IOException,
                        URISyntaxException {

                addAttributeToEmailTemplate(emailTestTemplate, "firstName", "John");
                addAttributeToEmailTemplate(emailTestTemplate, "lastName", "Wracker");
                String emailContent = ""; // provider email content here
                addAttributeToEmailTemplate(emailTestTemplate, "content", emailContent);
                addAttributeToEmailTemplate(emailTestTemplate, "senderFullName",
                                "Jeoff Desouza");
                return emailTestTemplate.render();
        }

        public void addAttributeToEmailTemplate(ST emailTemplate,
                        String attributeName, String attributeValue) {
                try {
                        emailTemplate.remove(attributeName);
                } catch (Exception e) {
                }

                emailTemplate.add(attributeName, attributeValue);
        }

}

email-Test.html

<html>
<head></head>
<body>
<br>  <br>
Dear
$firstName$
$lastName$
,<br>
<br>
<br>
<br>
<br>
$content$
<br>
<br>
<br>
Thanks & Regards , <b>
$senderFullName$<br>
<br>
</body>
</html>
Technology: 

Search