Home  >  Freemarker

Freemarker Shared Variable Example

By Arvind Rai, October 16, 2015
On this page I will provide Freemarker shared variable example. Shared variables are those variables that can be accessed in all Freemarker templates. What we have to do is that we will set variables in Freemarker configuration using setSharedVariable() method as follows.
configuration.setSharedVariable("globalMsg", "Hellow World!");
 
In our example, we have two Freemarker templates in which we will access shared variable.

Gradle to resolve Freemarker Dependency


build.gradle
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'
} 

Templates used in demo


template1.ftl
 ${globalMsg}
 
 My Name is: ${name} 

template2.ftl
 ${globalMsg}
 
 My Country is: ${country} 

Main Class


FreemarkerSharedVariableDemo.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.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
public class FreemarkerSharedVariableDemo {
	public static void main(String[] args) {
         try {
            //Instantiate Configuration class  
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
            cfg.setDirectoryForTemplateLoading(new File("ftl"));
            cfg.setDefaultEncoding("UTF-8");
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            cfg.setSharedVariable("globalMsg", "Hellow World!");
            //Create first Data Model 
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name", "Ram");
            Template template1 = cfg.getTemplate("template1.ftl");
            //Create second Data Model 
            Map<String, Object> map2 = new HashMap<>();
            map2.put("country", "India");
            Template template2 = cfg.getTemplate("template2.ftl");
            //Console output for template1
            Writer console = new OutputStreamWriter(System.out);
            System.out.println("----------Template 1------------");
            template1.process(map1, console);
            //Console output for template2            
            System.out.println("----------Template 2------------");
            template2.process(map2, console);
            console.flush();
            //File output
            Writer file1 = new FileWriter (new File("ftl/template1-output.html"));
            template1.process(map1, file1);
            file1.flush();
            file1.close();
            Writer file2 = new FileWriter (new File("ftl/template2-output.html"));
            template2.process(map2, file2);
            file2.flush();
            file2.close();            
         } catch (IOException e) {
            e.printStackTrace();
         } catch (TemplateException e) {
            e.printStackTrace();
        }
       }
} 

Output

Find the output.
 ----------Template 1------------
 Hellow World!
 
 My Name is: Ram
----------Template 2------------
 Hellow World!
 
 My Country is: India 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us