FreeMarker
By Apache Software Foundation
Apache FreeMarker is an open-source Java template engine that generates text output — commonly HTML, but also configuration files, emails, or source code — by combining a template written in its own template language with data supplied by…
Definition
Apache FreeMarker is an open-source Java template engine that generates text output — commonly HTML, but also configuration files, emails, or source code — by combining a template written in its own template language with data supplied by an application. It separates presentation markup from application logic by design, restricting templates to a controlled expression language rather than allowing arbitrary Java code inside them.
Overview
FreeMarker was created to address a common problem in server-side Java applications: mixing presentation markup with business logic, as early JSP scriptlets often did, makes templates hard to maintain and blurs responsibility between developers and page designers. FreeMarker's template language deliberately excludes general-purpose programming constructs like arbitrary method calls or object instantiation, offering instead a constrained set of directives for loops, conditionals, and variable interpolation, which keeps templates focused on presentation while application logic stays in Java code that prepares the data model passed to the template. Mechanically, a FreeMarker template uses its own syntax — `${...}` for interpolating values and `<#if>`, `<#list>`, and other `<#...>` directives for control flow — layered on top of the target output format, most often HTML but equally applicable to plain text, XML, or source code templates. A Java application builds a data model, typically a map of objects, and FreeMarker's processing engine merges that model with the template to produce the final output, with the template completely unaware of how the data was computed and the Java code unaware of exact markup details. Against Thymeleaf, FreeMarker's main structural difference is that its templates are not valid, directly viewable HTML on their own because its directives use a distinct tag syntax rather than attributes on standard HTML elements, so FreeMarker lacks Thymeleaf's natural-templating property. Against JSP, both aim to avoid heavy logic in templates, but FreeMarker was built from the start as a template language without a code-execution model, whereas JSP historically allowed embedded Java scriptlets that undermine that separation unless a team disciplines itself to avoid them. In practice, FreeMarker is used in server-side Java applications for rendering HTML views, generating email content, and producing configuration files or code from templates in build tooling, a use case beyond typical web frameworks where its lack of HTML-specific assumptions is an advantage. Apache Struts historically used FreeMarker as one of its supported view technologies, and it remains common in legacy Java web applications built before Thymeleaf's rise as the Spring ecosystem's default. FreeMarker's restricted expression language, while a deliberate design choice for separation of concerns, can feel limiting for developers who need more complex logic in a view and end up working around the restriction rather than moving that logic into the controller layer as intended. Its lack of natural templating also means designers cannot preview templates directly in a browser the way they can with Thymeleaf, which affects some team workflows.
Key Features
- Own template language distinct from the target output format's syntax
- Restricted expression language deliberately excludes arbitrary code execution
- Generates HTML, plain text, XML, emails, or source code from templates
- Data model built in Java code is merged with templates at render time
- Directives for loops, conditionals, and variable interpolation
- Used historically as a view technology option in Apache Struts
- Independent of any specific web framework, usable in any Java application
- Mature, long-standing Apache project with stable template syntax