Sunday, 5 July 2020

FreeMarker: StringTemplteLoader: Load templates from strings

‘StringTemplateLoader’ uses a Map with String-s as its source of templates.

 

Follow the below step-by-step procedure to process a template using StringTemplateLoader.

 

Step 1: Define templates.

String copyRightTemplate = "---------------------------------------------------------------------------------\n"
        + "All these examples are owned by https://self-learning-java-tutorial.blogspot.com/\n"
        + "You can use these examples and share to others also\n" + "\n" + "Author: Krishna\n"
        + "Date: ${today?datetime}\n"
        + "---------------------------------------------------------------------------------";

String welcomeTemplate = "<#-- Include Demo -->\n" + "\n" + "<#include \"/copyright\">\n" + "\n"
        + "Hello ${name}, Welcome to our Application.";

Step 2: Load the templates to StringTemplateLoader.

StringTemplateLoader stringLoader = new StringTemplateLoader();

stringLoader.putTemplate("copyright", copyRightTemplate);

stringLoader.putTemplate("welcomeTemplate", welcomeTemplate);

 

Step 3: Get the instance of Configuration and set the template loader.

Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);

configuration.setTemplateLoader(stringLoader);

 

Step 4: Define Root Object.

 

Map<String, Object> rootObject = new HashMap<>();

 

rootObject.put("today", new Date());

rootObject.put("name", "Krishna");

 

Step 5: Get the template.

Template template = configuration.getTemplate("welcomeTemplate");

 

Step 6: Process the template using rootObject.

StringWriter stringWriter = new StringWriter();

template.process(rootObject, stringWriter);

 

Find the below working application.

StringTemplateLoaderDemo.java

package com.sample.app;

import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class StringTemplateLoaderDemo {
    public static void main(String args[]) throws Exception {

        String copyRightTemplate = "---------------------------------------------------------------------------------\n"
                + "All these examples are owned by https://self-learning-java-tutorial.blogspot.com/\n"
                + "You can use these examples and share to others also\n" + "\n" + "Author: Krishna\n"
                + "Date: ${today?datetime}\n"
                + "---------------------------------------------------------------------------------";

        String welcomeTemplate = "<#-- Include Demo -->\n" + "\n" + "<#include \"/copyright\">\n" + "\n"
                + "Hello ${name}, Welcome to our Application.";

        StringTemplateLoader stringLoader = new StringTemplateLoader();
        stringLoader.putTemplate("copyright", copyRightTemplate);
        stringLoader.putTemplate("welcomeTemplate", welcomeTemplate);

        Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
        configuration.setTemplateLoader(stringLoader);

        Map<String, Object> rootObject = new HashMap<>();

        rootObject.put("today", new Date());
        rootObject.put("name", "Krishna");

        Template template = configuration.getTemplate("welcomeTemplate");

        StringWriter stringWriter = new StringWriter();
        template.process(rootObject, stringWriter);

        System.out.println(stringWriter.toString());

    }
}

Output

---------------------------------------------------------------------------------
All these examples are owned by https://self-learning-java-tutorial.blogspot.com/
You can use these examples and share to others also

Author: Krishna
Date: 24 May, 2020 12:43:14 AM
---------------------------------------------------------------------------------
Hello Krishna, Welcome to our Application.



Previous                                                    Next                                                    Home

No comments:

Post a Comment