Description #
FreeMarker is a powerful Java-based templating engine used to generate dynamic HTML, configuration files, source code, and more, primarily in Java web applications.
History #
FreeMarker was created in 2000 by Jonathan Revusky and later maintained by the Apache Software Foundation. It was designed to separate logic from presentation in Java-based MVC architectures and became a core part of frameworks like Spring MVC.
Hello World Code #
<h1>${message}</h1>
With context:
Map<String, Object> data = new HashMap<>();
data.put("message", "Hello, World!");
How to Run #
Option 1: Online
Use a FreeMarker preview tool:
https://try.freemarker.apache.org/
Option 2: Local
Add to a Java project:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
Render in Java:
Template template = cfg.getTemplate("hello.ftl");
template.process(data, new OutputStreamWriter(System.out));
Key Concepts #
- Syntax:
${}
for expressions,<#...>
for logic - Template files use
.ftl
extension - Used in Java frameworks like Spring, Struts, Spark
- Supports conditionals, loops, macros, includes
- Data model passed as a
Map
or POJO - Safe HTML escaping options
- Can generate any text format (HTML, XML, etc.)
- Extensible via directives and custom methods
- Fast performance with precompiled templates
- Apache-licensed and widely supported
Try It Online #
https://try.freemarker.apache.org
Fun Facts #
- The “Free” in FreeMarker refers to freedom, not price—though it is both.
- Used by many CMS platforms and Java web servers to render user-customizable views.
- The syntax is similar to Velocity but more robust and easier to extend.
Resources #
Official site
Docs or tutorial
GitHub or interpreter
Community or learning resources