A few feature highlights of FreeMarker.
- Can be used to generate any kind of text: HTML, XML, RTF, Java source code, etc.
- Easy to embed into your product: Lightweight, no required dependencies. Doesn't assume servlet environment.
- Pluggable template loader: you can load templates from any sources; local files, database, etc.
- You can do whatever you want with the generated text: store it in a local file, send it as e-mail, send it back to a Web browser from a Web application, etc.
Powerful template language
- All the usual directives: if/elseif/else, looping through lists, include other templates.
- Creating and changing variables in templates.
- Can use complex expressions to specify values almost everywhere.
- String operations: concatenation, sub-string, uppercase, capitalize, escaping, etc.
- Decimal precision arithmetic calculations
- Boolean arithmetic
- Reading array and associative array elements
- Array and associative array literals
- You can add your own functions (methods) for special calculations.
- Custom directives (macros) with named and positional parameters and with nested content (body). For example:
<@myMacro color="red" width=2>...</@myMacro>
These can be defined right in the templates, or even in Java.
- Custom directives can filter the output of their nested content, such as doing white-space compression, syntax highlighting, etc.
- Name-spaces to help build and maintain reusable macro libraries or to divide big projects into separated modules without worrying about name clashes.
Versatile data model
FreeMarker does not work through direct reflection on Java objects; the Java objects are exposed to the template as a tree of variables through pluggable object wrappers.
Thus, you can show the objects (Java beans, XML documents, SQL query result sets, etc.) in an abstract, tailored way for the template authors, without bothering them with technical details.
This also means that objects coming for non-Java JVM languages (such as Jython) can be accessed as conveniently as "native" objects (if the proper object wrapper is implemented).
- Designed for MVC pattern: separate visual design from application logic.
- Built-in constructs in the template language to handle typical Web-related tasks like HTML-escaping.
- Can be integrated with "Model 2" Web application frameworks out-of-the-box as JSP replacement.
- Supports JSP taglibs (tested up to JSP 2.1 currently) via FreemarkerSerlvet, except that the JSP 2 "tag files" (i.e., when custom tags are implemented in JSP language rather than as Java classes) aren't supported.
- Charset-aware (uses UNICODE internally).
- Locale sensitive number and date/time formatting.
- Non-US characters can be used in identifiers (as variable names).
- Multiple variations of the same template for different languages.
XML Processing Capabilities
- Terse way of walking XML nodes, like book.chapter.title.
- Declarative/recursive XML processing (with the <#recurse> and <#visit> directives).