Using a Confluence page as a base page for a user macro

If you’re using Confluence (like I do at work) and have been digging around the user macros feature, this might be something for you.

I wanted to create a user macro, which basically just inserts a parametrized confluence page. Googling for it, I didn’t find a thing noted about how one would create such a macro. So, now there is.

A Confluence user macro is basically a Velocity template with parameters, that are evaluated by Confluence and presented to the user in a nice UI. These parameters are stuck in „$param<Name>“ variables, that can be used in the template.

So, basically you first have to create a Confluence page somewhere and use that kind of parameters there (note the prefix $param!). Please check the storage format after saving your page and check, that the parameters really are in the text like they should. (I came across a problem when I entered a parameter and edited it later and Confluence threw in a „<span>“-Tag in that)

After that, create a user macro and enter these basic code:

## Specify the page, this macro is based on

#set($key="SPACE")
#set($name="Page")

## Specify the parameters for that page

## (...)

## Get PageManager object instance
#set($containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)
#set($pageManager=$containerContext.getComponent('pageManager'))
 
## Get the Page object instance
#set($page=$pageManager.getPage($key,$name))

## Render the page with the parameters above
#evaluate($page.getBodyContent().getBody())

Replace the words SPACE and Page with your actual Space- and Pagenames. Finally, define the parameters for that page at the (…)-place in the code and you’re ready to go.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.