Wednesday, 14 January 2015

FreeMarker


Freemarker is a java based template engine.  In FreeMarker you define templates, which are text files that contain the desired output, except that they contain placeholders like ${name}, and even some logic like conditionals, loops, etc. In your Java program you supply the actual values for these placeholders and the final output is generated based on this input.

I am going to explain simple example, which displays list of employees.

hello.ftl

<html>
<head>
  <title>Hello File Marker</title>
</head>
<body>
  <ul>
    <#list employees as employee>
      <li>${employee.firstName}  ${employee.lastName}</li>
    </#list>
  </ul>
</body>
</html>


package com.freemarker;

public class Employee {

 private String firstName;
 private String lastName;
 
 Employee(String firstName, String lastName){
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

}

package com.freemarker;

import java.io.File;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class MainTest {

 public static void main(String[] args) throws Exception {
  /* The main entry point into the FreeMarker API;
   *  encapsulates the configuration settings of FreeMarker, 
   *  also serves as a central template-loading and caching service. */
  Configuration cfg = new Configuration();
  
  
  Map<String, List<Employee>> input = new HashMap<String, List<Employee>> ();
  
  List<Employee> employees = new ArrayList<Employee>();
  employees.add(new Employee("Hari Krishna", "Gurram"));
  employees.add(new Employee("Keerthi", "S"));
  employees.add(new Employee("Upasana", "Ads"));
  employees.add(new Employee("Ankit", "Suyal"));
  input.put("employees", employees);

  /* Retrieves the template with the given name */
  Template template = cfg.getTemplate("src\\hello.ftl");

  Writer consoleWriter = new OutputStreamWriter(System.out);
  Writer fileWriter = new FileWriter(new File("output.html"));
  
  /* Executes template, using the data-model provided, writing the generated output to the supplied Writer.  */
  template.process(input, consoleWriter);

  try {
   template.process(input, fileWriter);
  } finally {
   fileWriter.close();
  }

 }
}


Total structure looks like below.


Run MainTest.java, you will get output like below in console.

<html>
<head>
  <title>Hello File Marker</title>
</head>
<body>
  <ul>
          <li>Hari Krishna  Gurram</li>
      <li>Keerthi  S</li>
      <li>Upasana  Ads</li>
      <li>Ankit  Suyal</li>
  </ul>

</body>
</html>

Program also generates “output.html” file. “output.html” looks like below


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment