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:

  1. Select the Create Macro button on the Macro page. 

  2. Select Custom Script Macro.
  3. Set the following fields:

    • Key: message-macro

    • Name: Message

    • Description: Renders an AUI message

    • Body Type: Rich text

    • Output Type: Block

  4. 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. 

  5. Select the +Parameter button and set the following fields on the form that appears:
    1. Parameter Type: enum

    2. Name: level

    3. Label: Level

    4. Tick the checkbox for Required

      Description and Default are not required for this example. 

  6. Select the +enum value button, and then enter info
  7. Select the +enum value button, and then enter warning.
  8. 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.

  9. 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 of mkp.yieldUnescaped is safe because Confluence does not allow script tags or other potentially malicious HTML in the macro’s body 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.

  10. Skip the Macro Javascript Code, Macro CSS Style, and Lazy Loaded fields. 
  11. Click Add.
  12. 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. 

This screen appears because parameters were marked as required in the Parameter section.

When you select Insert, you can see the macro on the page and fill out the body of the message.

On this page