FreeMarker Template Hello World Example

1. Introduction to FreeMarker

FreeMarker is a Java based template engine which can be used as stand-alone or servlet bases Java programs. You write a FreeMarker template in a plain text file with the place holders like ${id}. This file is saved with .ftl extension. Not only the simple text, we can also use the conditions, loops, switch statements, etc. in the templates that can be dynamically supplied by the Java program. The Java program passes the actual value for the place holders defined in the FreeMarker template files.

The output of the template is written to the Writer object which you have provide and then this object can be passed to HTTP, File, etc. for printing the result. Programmer can configure the location of the templates and where the files has to be created with the results. This tutorial explains on how to create a simple FreeMarker template implementation in the stand alone mode.

2. How to Install FreeMarker?

Installing FreeMarker template engine is as simple as adding a JSR files to your project. You can download the latest version of the lobraries from freemarker website. At this time of writing this article, the latest version available for download is 2.3.20. If you are using the Maven build for your project, the add the following entry in your POM.xml.

Attention: Be sure nothing pulls in an old dependency with groupId
"freemarker" (without the "org."), because then you will end up with
two freemarker.jar-s and unpredictable behavior!


3. FreeMarker Java Template Processing

This is the important part of supplying the template place holders to the actual templates.


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class FreeMarkerHelloWorld {

	public static void main(String[] args) {

		// Configurae Freemarker
		Configuration cfg = new Configuration();
		try {
			// Load the template
			Template template = cfg.getTemplate("src/template.ftl");

			Map<String, Object> data = new HashMap<String, Object>();
			data.put("message", "Hello World!");

			// Language list
			List<String> language = new ArrayList<String>();

			data.put("languages", language);

			Writer out = new OutputStreamWriter(System.out);
			template.process(data, out);
		} catch (IOException e) {
		} catch (TemplateException e) {

4. Create FreeMarker Template

FreeMarker Hello World Example : ${message}

<#list languages as language>
    ${language_index + 1}. ${language}

5. Run Sample Application

If you run the above program, you would get the following output.

<pre>FreeMarker Hello World Example : Hello World!

1. Java
2. C++
3. Ceylon
4. Cobol

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>