Rendering Velocity templates in a macro

When writing a macro plugin , it's a common requirement to render a Velocity file included with your plugin. The Velocity file should be rendered with some data provided by your plugin.

The easiest way to render Velocity templates from within a macro is to use VelocityUtils.getRenderedTemplate and simply return the result as the macro output. You can use it like this:

public String execute(Map params, String body, RenderContext renderContext) throws MacroException {
    // 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);

Macro Module

Was this page helpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport