Freemarker TemplateLoader with MultiTemplateLoader Example
October 18, 2015
On this page we will provide Freemarker TemplateLoader with MultiTemplateLoader example. freemarker.cache.MultiTemplateLoader is the collection of other TemplateLoader class instances. These TemplateLoader classes are ClassTemplateLoader, StringTemplateLoader, FileTemplateLoader etc. In our example we are using ClassTemplateLoader and StringTemplateLoader. We will instantiate these loaders and will put in array of TemplateLoader using which MultiTemplateLoader will be instantiated. Now using getTemplate() method of Configuration class, we will be able to access all templates loaded by ClassTemplateLoader and StringTemplateLoader as usual. Find the complete example.
build.gradle
Find the gradle file to resolve Freemarker JAR dependency.apply plugin: 'java' apply plugin: 'eclipse' archivesBaseName = 'Concretepage' version = '1.0-SNAPSHOT' repositories { maven { url "https://repo.spring.io/libs-release" } mavenLocal() mavenCentral() } dependencies { compile 'org.freemarker:freemarker:2.3.23' }
Template for ClassTemplateLoader
Find the template for ClassTemplateLoader.template1.ftl
College : ${college} Location : ${location}
Freemarker Configuration with TemplateLoader and MultiTemplateLoader
We are using ClassTemplateLoader and StringTemplateLoader which will be added in MultiTemplateLoader in our demo.ConfigurationUtil.java
package com.concretepage.freemarker; import freemarker.cache.ClassTemplateLoader; import freemarker.cache.MultiTemplateLoader; import freemarker.cache.StringTemplateLoader; import freemarker.cache.TemplateLoader; import freemarker.template.Configuration; import freemarker.template.TemplateExceptionHandler; public class ConfigurationUtil { private static Configuration configuration ; static { configuration = new Configuration(Configuration.VERSION_2_3_23); //First loader ClassTemplateLoader classLoader = new ClassTemplateLoader( new ConfigurationUtil().getClass(), "/ftl"); //Second loader StringTemplateLoader stringLoader = new StringTemplateLoader(); stringLoader.putTemplate("template2.ftl", "Hello! My name is ${name} and I am from ${country}"); // Create TemplateLoader array TemplateLoader[] loaderArray = {classLoader, stringLoader}; //Instantiate MultiTemplateLoader MultiTemplateLoader loader = new MultiTemplateLoader(loaderArray); //Set to Configuration configuration.setTemplateLoader(loader); configuration.setDefaultEncoding("UTF-8"); configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); } public static Configuration getConfiguration() { return configuration; } }
Main Class
MultiTemplateLoaderDemo.java
package com.concretepage.freemarker; 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.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class MultiTemplateLoaderDemo { public static void main(String[] args) { try { Configuration cfg = ConfigurationUtil.getConfiguration(); //Create Data Model for first template Mapmap1 = new HashMap<>(); map1.put("college", "ABC College"); map1.put("location", "Varanasi"); Template template1 = cfg.getTemplate("template1.ftl"); //Create Data Model for second template Map map2 = new HashMap<>(); map2.put("name", "Ram Chandra"); map2.put("country", "India"); Template template2 = cfg.getTemplate("template2.ftl"); Writer console = new OutputStreamWriter(System.out); //Console output for first template System.out.println("--Console output for first template--"); template1.process(map1, console); console.flush(); //Console output for second template System.out.println("\n--Console output for second template--"); template2.process(map2, console); console.flush(); //File output Writer file1 = new FileWriter (new File("template-output1.txt")); template1.process(map1, file1); file1.flush(); file1.close(); Writer file2 = new FileWriter (new File("template-output2.txt")); template2.process(map2, file2); file2.flush(); file2.close(); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } } }
--Console output for first template-- College : ABC College Location : Varanasi --Console output for second template-- Hello! My name is Ram Chandra and I am from India