This example demonstrates how you can pass parameters into your macro. Let's say you want to write your own font colour macro, with a parameter allowing the user to specify the colour. Then perhaps you want to add another parameter, that allows the user to specify the font size.
For full details about creating a user macro, see the guide to writing user macros.
Defining the 'Colour' user macro
This example uses a single parameter.
To create the 'Colour' user macro:
- Choose the cog icon
at top right of the screen, then choose Confluence Admin.
- Choose User Macros in the left-hand panel.
- Choose Create a User Macro at the bottom of the list of macros.
- Enter the macro attributes as follows:
- Macro Name:
colour
- Visibility:
Visible to all users in the Macro Browser
- Macro Title:
Colour
- Description:
Colours a block of text
- Categories:
Confluence Content
- Icon URL: You can leave this field empty.
- Documentation URL: You can leave this field empty.
- Macro Body Processing:
Rendered
Template:
## @param 0:title=colour|type=string <span style="color: $param0">$body</span>
- Macro Name:
- Choose Save.
The information on this page does not apply to Confluence OnDemand.
Using the 'Colour' macro on a page
To add the macro to a page, edit the page and choose Insert > Other Macros and find the 'Colour' macro. (Or use autocomplete: start typing '{colo' in the editor, and select the 'Colour' macro from the list of suggestions that appears.)
Defining the 'Stylish' user macro
If your macro requires more than one parameter, you can use variables $param0 to $param9 to represent them. Let's say that you want to add a parameter that allows the user to specify the size of the text.
Enter the macro attributes as follows:
- Macro Name:
stylish
- Visibility:
Visible to all users in the Macro Browser
- Macro Title:
Stylish
- Description:
Applies colour and size to text
- Categories:
Confluence Content
- Icon URL: You can leave this field empty.
- Documentation URL: You can leave this field empty.
- Macro Body Processing:
Rendered
Template:
## @param 0:title=colour|type=string ## @param 1:title=size|type=string <span style="color: $param0; font-size: $param1">$body</span>
Naming your parameters
Alternatively, you can also use explicitly-named parameters in your macro. These macro parameters will appear as variables with the name $param<x> where <x> is the name of your parameter.
## @param Colour:title=colour|type=string ## @param Size:title=size|type=string <span style="color: $paramColour; font-size: $paramSize">$body</span>