The 'template' is one of the fields that you will define when creating a user macro. (See the rest of the guide to writing user macros.) This page gives you guidelines about the code you can enter in a user macro template.
The rest of this page gives more details of the above procedure.
On this page:
If you chose an output format of 'HTML', you must write the content of the template using HTML. In addition, you can use the Velocity templating language. For more information about the Velocity template language, see the Velocity user's guide.
If you chose an output format of 'Wiki Markup', you must write the content of the template using Confluence wiki markup.
Note: When coding in wiki markup, you can use $body, $paramFoo and Velocity too, as described in detail below. For example:
Foo $!paramFoo Honk
$body
Bar
$action.getText('done.name')
Eek
|
Use the $body object within your user macro template to access the content passed to your macro in the macro body.
The $body object is available if you have specified that your macro has a body (in other words, if you have not selected 'No macro body').
Example: Let's assume your macro is called helloworld.
Enter the following code in your template:
Hello World: $body |
A user enters the following on a Confluence page:
{helloworld}From Matthew{helloworld}
|
The wiki page will display the following:
Hello World: From Matthew
You can specify parameters for your macro, so that users can pass it information to determine its behaviour on a Confluence page.
When adding a macro to a Confluence page, users can pass parameters to your user macro in the same way as any other macro:
In summary, a parameter definition contains:
@paramFormat:
## @param MYNAME:title=MY TITLE|type=MY TYPE|desc=MY DESCRIPTION|required=true|multiple=true|default=MY DEFAULT VALUE |
Additional notes:
The sections below describe each of the attributes in detail.
Attribute Name |
Description |
Required / Recommended / Optional |
|---|---|---|
(an unnamed, first attribute) |
A unique name for the parameter. The parameter name is the first attribute in the list. The name attribute itself does not have a name. See the section on name below. |
Required |
title |
The parameter title will appear in the Macro Browser. If you do not specify a title, Confluence will use the parameter name. |
Recommended |
type |
The field type for the parameter. See the section on type below. |
Recommended |
desc |
The parameter description will appear in the Macro Browser. |
Optional |
required |
Specifies whether the user must enter information for this parameter. Defaults to 'false'. |
Optional |
multiple |
Specifies whether the parameter accepts multiple values. Defaults to 'false'. |
Optional |
default |
The default value for the parameter. |
Optional |
The parameter name is the first attribute in the list. The name attribute itself does not have a name.
Example: The following code defines 2 parameters, named 'foo' and 'bar':
## @param foo ## @param bar |
The field type for the parameter. If you do not specify a type, the default is string.
Parameter Type |
Description |
|
|---|---|---|
boolean |
Displays a checkbox to the user and passes the value 'true' or 'false' to the macro as a string. |
|
enum |
Offers a list of values for selection. You can specify the values to appear in a dropdown in the Macro Browser. Example of specifying the enum values:
Note about i18n: Confluence does not support internationalisation of the enum values.The value the user sees is the one passed to the macro as the parameter value, with the capitalisation given. In this case 'Grey', 'Red', etc. |
|
string |
A text field. This is the default type. Example with a required field:
|
|
confluence-content |
Offers a control allowing the user to search for a page or blog post. Example:
|
|
username |
Search for user.
|
|
spacekey |
Offers a list of spaces for selection. Passes the space key to the macro. Example:
|
|
date |
Confluence accepts this type, but currently treats it in the same way as 'string'. Example:
Note about dates: A user can enter a date in any format, you should validate the date format in your user macro. |
|
int |
Confluence accepts this type, but currently treats it in the same way as 'string'. Example with a default value:
|
|
percentage |
Confluence accepts this type, but currently treats it in the same way as 'string'. Example:
|
The parameters are available in your template as $paramfoo, $parambar for parameters named "foo" and "bar".
This example shows how the parameters would be used on a wiki page: {macro:foo=value|bar=value}.
Normally, a parameter like $paramfoo that is missing will appear as '$paramfoo' in the output. To display nothing when a parameter is not set, use an exclamation mark after the dollar sign like this: $!paramfoo
If your macro does not accept parameters, you should use @noparams in your template. That will let Confluence know that it need not display a parameter input field in the Macro Browser.
If the user macro contains no parameters and does not specify @noparams, then the Macro Browser will display a free-format text box allowing users to enter undefined parameters. This can be confusing, especially if the macro does not accept parameters.
Example: Add the following line at the top of your template:
## @noparams |
As well as the macro body and parameters, there are a variety of Confluence objects available in the Velocity context. In addition to the default context, user macros also include the following:
Variable |
Description |
Class Reference |
|---|---|---|
|
The body of the macro (if the macro has a body) |
String |
|
Named parameters ("foo", "bar") passed to your macro. |
String |
|
The |
|
|
The |
|
|
The |
|
|
The current |
For a list of objects available in the default Velocity context, see the developer documentation.
Writing User Macros
Examples of User Macros