Velocity Engine

Docs

Developers

Community

Translations (outdated)

Velocity News Feed

Overview

Velocity is a template engine that can be used for many purposes. Some common types of applications which use Velocity are:

  • Web applications. Web designers create HTML pages with placeholders for dynamic information. The page is processed with VelocityViewServlet or any of a number of frameworks which support Velocity. This approach to web application development is called Model-View-Controller or MVC and is intended to be a direct replacement for applications developed with Java Server Pages (JSPs) or PHP.
  • Source code generation. Velocity can be used to generate Java source code, SQL, or PostScript based on templates. The PoweredByVelocity page lists a number of open source and commercial development software packages which use Velocity in this manner.
  • Automatic emails. Many applications generate automatic emails for account signup, password reminders or automatically sent reports. Using Velocity, the email template can be stored in a text file rather than directly embedded in your Java code.
  • XML transformation. Velocity provides an ant task called Anakia which reads an XML file and makes it available to a Velocity template. A common application is to convert documentation stored in a generic "xdoc" format into a styled HTML document.

How it works

Velocity allows web page designers and other template writers to include markup statements called references in the page. These references are pulled from a Context object -- essentially a hashtable that provides get and set methods for retrieving and setting objects -- and the corresponding values are inserted directly in a page. Velocity provides basic control statements, that can loop over a collection of values (foreach) or conditionally show a block of text (if/else). The ability to call arbitrary Java methods, include other files, and to create macros that can be repeatedly used make this a powerful yet easy-to-use approach for creating dynamic web page or other text files.

Velocity enforces a Model-View-Controller (MVC) style of development by separating Java code from HTML template code. Unlike JSPs, Velocity does not allow Java code to be embedded in pages. Unlike PHP, Velocity does not implement features with other functions. The MVC approach is one of Velocity's great strengths, and allows for more maintainable and better-designed web pages.

Although MVC-style development can sometimes lead to longer incubation periods for web sites, particularly if the developers involved are new to MVC, this approach saves time over the long term (believe us, we have been doing this for a long time now). The MVC abstraction prevents web page designers from messing with a software engineer's Java code, and programmers from unduly influencing the look of web sites. Velocity enforces a contract that defines what roles people play in the web site development process.

Extending Velocity's Capabilities

While Velocity is generally useful within an application as is, there are a number of ways its capabilities can be extended.

  • Special types of objects, generically called "Tools", contain methods but no data. When placed into the Velocity context the template can call these methods to do basic tasks like formatting numbers or escaping HTML entities.
  • Velocity provides a selection of resource loaders that can retrieve templates from text files, the classpath, even a database. But if that's not enough you can write your own resource loader to retrieve in a custom manner.
  • Event Handlers provide hooks to perform custom actions upon certain events, such as the insertion of a reference into text.
  • Advanced users can write a custom Introspector which retrieves reference properties and methods. For example, you might create an introspector that retrieves data from Lucene or other search engine indexes.
  • Finally, for the truly adept, the grammar for Velocity is processed in a parser generated by JavaCC (Java Compiler Compiler) using the JJTree extension to create an Abstract Syntax Tree. By changing the JavaCC specification file and recompiling, the Velocity syntax itself can be changed.

Acknowledgement

Velocity's design concept is borrowed from WebMacro. Those involved in the Velocity project acknowledge and appreciate the development and design work that went into the WebMacro project.