Freemarker FileTemplateLoader and StringTemplateLoader Example

By Arvind Rai, October 17, 2015
This page will walk through Freemarker FileTemplateLoader and StringTemplateLoader example. FileTemplateLoader loads templates from a specified directory and StringTemplateLoader defines a Map for templates. The key of Map is String that is the name of template file. To resolve JAR dependency we need to add Freemarker JAR as follows.
compile 'org.freemarker:freemarker:2.3.23' 

FileTemplateLoader

freemarker.cache.FileTemplateLoader loads templates from a specified directory. The instance of it is added to Configuration using setTemplateLoader() method. Find the code snippet to understand it.
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
FileTemplateLoader loader = new FileTemplateLoader(new File("ftl"));
cfg.setTemplateLoader(loader); 

Template used in Demo

For the example, we have a template that we are using in our demo.
template1.ftl
Name : ${name}

Location : ${location}  

Main Class


FileTemplateLoaderDemo.java
package com.concretepage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
public class FileTemplateLoaderDemo {
	public static void main(String[] args) {
          try {
            //Instantiate Configuration class  
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
            FileTemplateLoader loader = new FileTemplateLoader(new File("ftl"));
            cfg.setTemplateLoader(loader);
            cfg.setDefaultEncoding("UTF-8");
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            //Create first Data Model 
            Map<String, Object> map = new HashMap<>();
            map.put("name", "Ram");
            map.put("location", "Ayodhya");
            Template template = cfg.getTemplate("template1.ftl");
            //Console output for template
            Writer console = new OutputStreamWriter(System.out);
            template.process(map, console);
            console.flush();
            //File output
            Writer file1 = new FileWriter (new File("ftl/template1-output.html"));
            template.process(map, file1);
            file1.flush();
            file1.close();
          } catch (IOException e) {
            e.printStackTrace();
          } catch (TemplateException e) {
            e.printStackTrace();
          }
       }
} 
Find the output.
Name : Ram

Location : Ayodhya  

StringTemplateLoader

freemarker.cache.StringTemplateLoader uses a Map with string as its source of templates and then we can access templates as usual by getTemplate() method of Freemarker Configuration. Find the code snippet. Here we are adding to templates in our StringTemplateLoader and then we will process it.
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("nameTemplate", "Name : ${name}");
stringLoader.putTemplate("locationTemplate", "Location: ${location}");
cfg.setTemplateLoader(stringLoader); 
The above code prepares two templates nameTemplate and locationTemplate. Now find the complete example.
StringTemplateLoaderDemo.java
package com.concretepage;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
public class StringTemplateLoaderDemo {
	public static void main(String[] args) {
          try {
            //Instantiate Configuration class  
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
            StringTemplateLoader stringLoader = new StringTemplateLoader();
            stringLoader.putTemplate("nameTemplate", "Name : ${name}");
            stringLoader.putTemplate("locationTemplate", "Location: ${location}");
            cfg.setTemplateLoader(stringLoader);
            cfg.setDefaultEncoding("UTF-8");
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            //Create Data Models 
            Map map1 = new HashMap<>();
            map1.put("name", "Ram");
            Template template1 = cfg.getTemplate("nameTemplate");
            Map map2 = new HashMap<>();
            map2.put("location", "Ayodhya");
            Template template2 = cfg.getTemplate("locationTemplate");
            //Console output for templates
            Writer console = new OutputStreamWriter(System.out);
            System.out.println("--Result for nameTemplate--");
            template1.process(map1, console);
            System.out.println("\n--Result for locationTemplate--");            
            template2.process(map2, console);
            console.flush();
          } catch (IOException e) {
            e.printStackTrace();
          } catch (TemplateException e) {
            e.printStackTrace();
          }
       }
} 
Find the output.
--Result for nameTemplate--
Name : Ram
--Result for locationTemplate--
Location: Ayodhya 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us