Create a Macro with Parameters
You can set up a macro that accepts parameters.
AUI Message Macro
Follow these steps to create a macro that outputs an AUI message:
Select the Create Macro button on the Macro page.
- Select Custom Script Macro.
Set the following fields:
Key: message-macro
Name: Message
Description: Renders an AUI message
Body Type: Rich text
Output Type: Block
Select the + Parameter button and set the following fields on the form that appears:
Parameter Type: string
Name: title
Label: Title
Tick the checkbox for Required
Description and Default are not required for this example.
- Select the +Parameter button and set the following fields on the form that appears:
Parameter Type: enum
Name: level
Label: Level
Tick the checkbox for Required
Description and Default are not required for this example.
- Select the +enum value button, and then enter info.
- Select the +enum value button, and then enter warning.
Select the +enum value button, and then enter error.
The form should look after you've added your Parameter fields:
The Parameter Types string and enum determine the two parameters of the macro, which allow editors using the macro to determine the following components of the message:
The title of the message is a string.
- The severity of the message is an enum because only a finite list of level types are accepted.
Find out more about the different parameter types here.
For Macro Code, you can type in the following code or put it in a file and enter the relative path to the file, under the script root (as usual).
import groovy.xml.MarkupBuilder def writer = new StringWriter() def builder = new MarkupBuilder(writer) builder.div('class': "aui-message aui-message-${parameters.level}") { p('class': 'title') { strong(parameters.title) } mkp.yieldUnescaped(body) } writer.toString()
The
mkp.yieldUnescaped
method is dangerous and should only be used with trusted data. In this particular case, the use ofmkp.yieldUnescaped
is safe because Confluence does not allow script tags or other potentially malicious HTML in the macro’sbody
variable. Other user inputs like the ones you specify as macro parameters are not checked in the same way and should not be trusted. It’s important that these inputs are handled securely, as discussed in our documentation on custom macros and security.- Skip the Macro Javascript Code, Macro CSS Style, and Lazy Loaded fields.
- Click Add.
- View your new macro when the Macro page loads.
Now you can take all ordinary macro actions.
Macro with parameters in use
When you select the macro to use on a page, this popup appears:
From here, you can fill out a Title for the message and assign an information type in the Level field.
When you select Insert, you can see the macro on the page and fill out the body of the message.