FreeMarker 2.3.8 review

Download
by rbytes.net on

FreeMarker is a "template engine"; a generic tool to generate text output (anything from HTML to autogenerated source code) based on

License: BSD License
File size: 0K
Developer: Jonathan Revusky
0 stars award from rbytes.net

FreeMarker is a "template engine"; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. FreeMarker is a Java package, a class library for Java programmers. It's not an application for end-users in itself, but something that programmers can embed into their products.

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 HMTL page author is the same person, since it helps keeping 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, and FreeMarker just generates textual pages that display the prepared data using templates.

FreeMarker is not a Web application framework. It is suitable for 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.

Here are some key features of "FreeMarker":
General purpose

Can be used to generate any kind of text: HTML, XML, RTF, Java source code, etc.
Easy to embed into your product: Lightweight. Does not require servlet environment (does not depend on javax.servlet classes).
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 usual directives: include, if/elseif/else, loop constructs.
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.
Macros with named and positional parameters and with nested content (body). For example:
< @myMacro color="red" width=2>...< /@myMacro >
Name-spaces to help build and maintain reusable macro libraries or to divide big projects into separated modules, without worrying about name clashes.
Output transformation blocks. Do transformations as HTML-escaping, compression, syntax-highlight etc. on the output generated by the nested template fragment. You can define your own transformations.

Versatile data model

FreeMarker does not work through direct reflection on Java objects; the Java objects are exposed to the template as 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.
Web-ready

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.
Designed for MVC pattern: separate visual design from application logic, separate page designers from programmers.

Internationalization/localization aware

Charset aware (uses UNICODE internally).
Locale sensitive number formatting.
Locale sensitive date and time formatting.
Non-US characters can be used in identifiers (as variable names).
Multiple variations of the same template for different languages.

Powerful XML Processing Capabilities

< #recurse > and < #visit > directives (new in version 2.3) for recursively walking an XML tree
Clean, intuitive XML object model accessible from template.

Comparing with other products

You might also be interested what distinguish FreeMarker from Apache Velocity.

What's New in This Release:
Changes on the Java side:

JSP support improvement: [1326058] Added support for DynamicAttributes (new in JSP 2.0)
JSP support improvement: Added support for pushBody()/popBody() in FreemarkerPageContext
JSP support improvement: Added support for getVariableResolver() (new in JSP 2.0).
JSP support improvement: Added support for include(String, boolean (new in JSP 2.0).
JSP support improvement: Added support for getExpressionEvaluator( (new in JSP 2.0). However, it will need Apache commons-el in the class path, or else this method will not work (it's optional). Note that EL support is not needed in principle, since FreeMarker has it's own expression language. But some custom JSP 2 tags may still want to use this method, after all it's in the JSP 2 API.

Changes on the FTL side: none

FreeMarker 2.3.8 keywords