Write a Macro in a Wiki Page
Last modified by Eleni Cojocariu on 2026/10/13 13:33
Tutorial
This tutorial builds a hello macro that greets the reader, from an empty page to a macro every page of the wiki can call. The macro itself does very little; what it walks through is the creation process, which is the same for every wiki macro. See Wiki Macros for what a wiki macro is and with which rights it runs.
You need to be an advanced user to reach the object editor.
- Create a page to hold the macro, and add an object of type XWiki.WikiMacroClass to it in the object editor.
- Fill in the fields of that object: hello in "Macro id", Hello in "Macro name", a short text in "Macro description", "Current Wiki" in "Macro visibility" and "Optional" in "Macro content availability". Leave the other fields at their default value, see Wiki Macro Fields.
- Write what the macro does in "Macro code", in the syntax of the page:
{{velocity}}Hello World!{{/velocity}}
- Save the page. The macro is available under its id at once, with nothing to restart.
- Call the macro from any page, the way every other macro is called:
{{hello/}} - View that page. It displays the greeting:

- Display the content the caller passes, by adding the {{wikimacrocontent}} macro to "Macro code". That content then becomes editable in place in the WYSIWYG editor, see Wiki Macros.
{{velocity}}Hello World!{{/velocity}} {{wikimacrocontent/}} - Add a parameter, by adding an object of type XWiki.WikiMacroParameterClass to the same page and setting its "Parameter name" to greetUser:

- Read that parameter in "Macro code", to greet the current user when it is set:
{{velocity}} #if ($wikimacro.parameters.greetUser && "XWiki.XWikiGuest" != "$xcontext.user") Hello $services.rendering.escape($xwiki.user.email, 'xwiki/2.1')! #else Hello world! #end {{/velocity}} - Call the macro with the parameter:
{{hello greetUser="true"/}} - View the page. The macro greets the current user, while the call without the parameter still displays the plain greeting:

FAQ
Why is $greetUser empty in my macro code?
Because a parameter is not bound as a variable of its own. Read it from $wikimacro.parameters.greetUser.
Why does the macro ignore the parameter I pass from a variable?
Because the variable is only resolved inside a script. Wrap the call in the velocity macro, otherwise the macro receives the name of the variable as text.