Last updated Apr 2, 2024

How to render a Velocity template from Java code

The best way to render a Velocity template from Java code is to use VelocityUtils. You'll need to provide VelocityUtils with the name of the template you want to render, and a map of parameters that will be made available within the template as $variables in Velocity.

Confluence has a default set of objects for Confluence Velocity templates. These are required for most Confluence Velocity macros to work properly. To obtain this context, you should call MacroUtils.defaultVelocityContext();. For example:

1
2
// Create the Velocity Context
Map context = MacroUtils.defaultVelocityContext();
context.put("myCustomVar", customVar);
context.put("otherCustomVar", otherCustomVar);
// Render the Template
String result = VelocityUtils.getRenderedTemplate("/com/myplugin/templates/macro.vm", context);

See MacroUtils for more information.

Warning

To keep Confluence secure, we strongly recommend you use Confluence's built in Velocity renderer, rather than your own renderer. This means you should not initialise your own VelocityEngine.

Rate this page: