Introduction to FreeMarker Template (FTL)

freemarker-logoFreeMarker is a Java-based template engine focusing on the MVC software architecture. Although it’s mostly used for Servlet-based Web Application development, it can be used for any other kind of text output, such as generating CSS, Java source code, etc. Unlike JSP, it is not dependent on the Servlet architecture or on HTTP. Thus it can be used for non-Web tasks as well. FreeMarker is Free software.

Following is the list of tutorials from Freemarker tutorial series.

It is a dynamic “On the Fly” template based text output rendering “language” which can output text in various formats. It is thus highly suitable wherever there is a need to customize the output in various formats and dynamically.

ftl-template-data-output

FreeMarker is designed to be practical for the generation of HTML Web pages, particularly by servlet-based applications following the MVC (Model View Controller) pattern. The idea behind using the MVC pattern for dynamic Web pages is that you separate the designers (HTML authors) from the programmers. Everybody works on what they are good at. Designers can change the appearance of a page without programmers having to change or recompile code, because the application logic (Java programs) and page design (FreeMarker templates) are separated. Templates do not become polluted with complex program fragments. This separation is useful even for projects where the programmer and the HTML page author is the same person, since it helps to keep the application clear and easily maintainable.

Although FreeMarker has some programming capabilities, it is not a full-blown programming language like PHP. Instead, Java programs prepare the data to be displayed (like issue SQL queries), and FreeMarker just generates textual pages that display the prepared data using templates.

freemarker-overview
Image Courtesy: http://freemarker.sourceforge.net/index.html

FreeMarker is not a Web application framework. It is suitable as a component in a Web application framework, but the FreeMarker engine itself knows nothing about HTTP or servlets. It simply generates text. As such, it is perfectly usable in non-web application environments as well. Note, however, that we provide out-of-the-box solutions for using FreeMarker as the view component of Model 2 frameworks (e.g. Struts), which also let you use JSP taglibs in the templates.

Features of FreeMaker

  • Has a versatile data model in Object Wrappers.
  • Has localization and internationalization. (l10n, i18n)
  • Configurable and extensible.
  • Generic – Output goes to any writer.
  • Dynamic template loading by freemarker engine.
  • Template loading from File, DB, Web, Jar etc.
  • XML, Ant support for variable substitution.
  • Name-spaces to help build and maintain reusable macro libraries or to divide big projects into separated modules, without worrying about name clashes.
  • Tag support for JSP tag libs with JSPSupportServlet.

FreeMarker comes with Built-in constructs in the template language to handle typical web related tasks like HTM-escaping. For example:

[#escape x as x?html]    html goes here   [/#escape]

The $ and curly brace is called interpolation. FreeMarker will replace it in the output with the actual value of the thing inside the curly brackets.

Interpolations :  ${ …} 

It also does transformations as HTML-escaping, compression, syntax-highlight etc. on the output generated by the nested template fragment. You can define your own transformations. FreeMarker uses complex expressions to specify values almost everywhere.

  • Variable is accessed by its name ${var_name}
  • Arithmetics +, -, / e.g. ${2+4-6}
  • Logical Operators &&, ||, <= etc.
  • Sequence slice ${profile.assets[1..]}
  • Include files [#include "header.html"]
  • Comments [#-- this is a comment --]

View Comparison: FreeMarker vs Velocity vs JSP

Following is just an highlevel comparison between FreeMarker, Velocity and JSP.

FeatureFreeMarkerVelocityJSP
Support for transparent i18n with numbers and dates shown according to the relevant localeYesNoYes
Support for JSP taglibsYesYesYes
Support for Jython and RhinoYesNoYes
Special tags for stripping of extraneous whitespaceYesNoYes, in jsp servlet of Tomcat add init-param trimSpaces
Support for auto-escaping interpolations on blocks of text (converting problematic characters to HTML entities, for example).YesNoYes
Allows methods to be called with argumentsYesYesNo
Templates can be loaded from a JAR on a remote serverYes-No
XML in variables is escaped by defaultYes-Yes, with a hack
Containerless, network-independent execution engineYesYesNo

This was just an introductory article on FreeMarker. In next tutorial we will create our first Hello World FreeMarker application. Stay tuned :)



8 Comments

  • Prakash Karunanidhi 12 June, 2012, 17:51

    Thanks for updating me through mail

  • Bob Ciesla 31 December, 2012, 22:05

    Do you do FreeMarker template coding? Do you have a website with other templates?

  • Deepak Garg 1 February, 2013, 21:13

    Can FTL page loaded or include from an external file system?
    Like
    Or any other way to load page from Physical location (D:/deepak/externalPage.ftl)?

    • Raza 12 August, 2013, 12:19

      Yennada Appadoo podu wellei Kalladi Summa . Thanx a lot

  • aniketh 2 July, 2013, 12:29

    nice example

  • eastwood 11 October, 2013, 11:46

    Look nice, i am studying the OFBIZ.12.4, and it use the freemarker. thanks.

  • Kamran 12 November, 2013, 23:40

    Nice work . Thanks for sharing it here.

  • patel 29 March, 2014, 11:14

    please post spark+freemarker tutorial

Leave a Reply

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

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current day month ye@r *