Below are some sample user macros. To see how to write a user macro and add it to your Confluence site, take a look at our guide to writing user macros. | On this page:
|
Simple Examples of User Macros
We provide these user macros as simple examples just to get you started. You would not want to install these user macros onto your Confluence site.
Example 1: User Macro to Display 'Hello World'
Take a look at an example of a 'Hello World' macro.
Example 2: The 'Error' User Macro to Create a Red Box
Let's write a simple macro that creates a red box (using an existing Confluence style) around some text. This may be useful for writing about error conditions, for example. That is why we give this macro the name 'error'.
To create the 'Error' user macro:
- Go to the 'Confluence Administration Console' and click User Macros in the left-hand panel.
- Click Create a User Macro at the top of the list of macros.
- Enter the macro attributes as follows:
- Macro Name:
error
- Visibility:
Visible to all users in the Macro Browser
- Macro Title:
Error
- Description:
Displays a red box around some 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:
<div class="error">$body</div>
- Macro Name:
- Click Add.
To use the macro within a page, use the Macro Browser. Your page will display an error box, like this:
This is bad
Example 3: User Macro to Demonstrate the Use of Parameters
This example demonstrates how you can pass parameters into your macro. Let's say you want to write your own font colour macro:
<span style="color: $param0">$body</span>
The usage of this macro will be:
{colour:green}Some example text{colour}
The output will be:
Some example text
If your macro requires more than one parameter, you can use variables $param0 to $param9 to represent them. To specify multiple parameters, use:
{colour:red|blue|green}
Where red, blue and green are the 1st, 2nd and 3rd parameters respectively.
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. To specify named parameters, use:
{style:colour=red}
In your user macro you can then use $paramcolour
which will have the value red
in this case.
User-Contributed User Macros
You may want to take a look at the library of user-contributed user macros.
Be careful when installing user macros from unknown authors.