Last updated Sep 25, 2019

Rendering Velocity templates in a macro

When writing a macro, you may want to render a Velocity file with data provided by your plugin.

The easiest way to render Velocity templates from within a macro is to use VelocityUtils.getRenderedTemplate, and return the result as the macro output. For example:

1
2
public String execute(Map<String, String> map, String s, ConversionContext conversionContext) throws MacroExecutionException {
    // do something with params ...

    Map context = MacroUtils.defaultVelocityContext();
    context.put("page", page);
    context.put("labels", labels);
    return VelocityUtils.getRenderedTemplate("com/atlassian/confluence/example/sample-velocity.vm", context);
}

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: