Writing XWiki Rendering Macros in wiki pages

Version 138.1 by Simon Urli on 2025/04/23

Wiki macros allow macro authors to develop reusable and distributable macro modules. There is no java code involved; hence no compiling or packaging. The macro author simply needs to create a wiki page according to a particular specification and that's all!

This page is a tutorial but you can also access the reference documentation for the Wiki Macro feature.

Hello Macro

We are going to start with a very simple xwiki/2.0 wiki macro which prints a greeting message to the document content. It isn't a very useful macro but the idea is to get you familiarised with the wiki macro creation process.

Definition

Wiki macros are defined using objects of type XWiki.WikiMacroClass. You define a wiki macro by creating a new wiki page and attaching to it an object of type XWiki.WikiMacroClass. You will need to be advanced to access the object editor.

Warning

There can be only one object of type XWiki.WikiMacroClass per wiki page (if you add more only the first will be used).

This class contains the following fields:

  • Macro id: Id of the macro to be used by users when invoking your macro from wiki code
  • Macro name: Name of the macro to be displayed on the wysiwyg editor
  • Macro description: A short description of the macro to be displayed on the WYSIWYG editor
  • XWiki 14.4+ Default categories: Default categories under which this macro should be listed (XWiki <14.4 It was called "Default category")
  • Supports inline mode: Whether the macro can be used in an inline context or not
  • Macro content availability: Whether this macro should support a body or not. Valid values are "Optional", "Mandatory" and "No Content".
  • Macro content type: The type of accepted content: two values are proposed, WIKI if this content should be editable like a wiki content, or UNKNOWN if it should be displayed like a plain text. It's also possible to specify a custom java type such as java.util.List<java.lang.String>. Leaving the field blank is equivalent to UNKWOWN value.
  • Content description: A short description about the macro's content to be displayed on the WYSIWYG editor
  • Macro code: The actual wiki code that will be evaluated when the macro is executed, can be any xwiki content (should be in the same syntax as the document)
  • Priority: The priority of execution relative to the other Macros. The lowest values have the highest priorities and execute first. For example a Macro with a priority of 100 will execute before one with a priority of 500. The default value is 1000.
  • XWiki 17.3.0+ Execution is isolated: If the execution of the macro doesn't modify the XDOM (including through the direct execution of other macros, e.g., parsed from the content or another page). Reading the XDOM and reading and writing other context information is okay. Marking the macro execution as isolated speeds up the execution of macros.
  • Asynchronous rendering: Enabled or disable asynchronous rendering of the macro. Disabled by default.
  • Cached: Indicate if the result of the execution of the element should be cached. Disabled by default.
  • Context elements: The context information required during the execution of the extension (current user, current document, etc.). It's also used to generate the cache key.

Now we can define our hello macro as shown below:

macro1.png

Invocation

A wiki macro can be invoked just like any other macro is invoked. Since we are writing a xwiki/2.1 wiki macro, we can invoke our hello macro as below:

{{hello/}}

And if you view the result it would say "Hello World!" (of course).

Content

The easiest way to insert the content of the wiki macro is to use a dedicated macro in the body of the wikimacro:

{{wikimacrocontent/}}

Note that by default this makes the content of the macro directly editable in the WYSIWYG editor.

For more details, see the Scripting Tips section below.

Parameters

Introducing a parameter to a wiki macro is pretty straight forward; you simply need to add an object of type XWiki.WikiMacroParameterClass into your wiki macro document (one object per parameter). This class contains several fields that allow you to define your parameter clearly:

  • Parameter name: Name of the parameter, users will refer this name when invoking your macro with parameters
  • Parameter description (optional): A short description of the parameter, this description will be made available on the WYSIWYG editor
  • Parameter mandatory: Indicates if this particular parameter is mandatory, wiki macro will fail to execute if a mandatory parameter is missing
  • Parameter default value (optional): The default value of the parameter when it's empty
  • Parameter type (optional): Indicates to which Java type the parameter value (defined as a String). Two values are proposed, WIKI if this parameter contains wiki content, or UNKNOWN if it should be displayed like a plain text. It's also possible to specify a custom java type such as java.util.List<java.lang.String>. Leaving the field blank is equivalent to UNKWOWN value.
    • This is needed when using the {{wikimacroparameter}} macro (see below), which uses this type to decide how to render the macro parameter.
    • For more information about parameter types and parameter pickers, see: Using pickers for XWiki Rendering Macro parameters
  • XWiki 16.10.6+, 17.3.0+ Parameter feature (optional): Allows to specify the name of the feature this parameter is bound to. If several parameters are bound to same feature, it means a choice need to be made between those parameters.
  • XWiki 16.10.6+, 17.3.0+ Parameter group (optional): Allows to specify the name of the group this parameter belongs to. This allows to visually shows the parameters together in the macro configuration UI.
  • XWiki 16.10.6+, 17.3.0+ Parameter hidden (optional): Indicates whether the parameter should be displayed or not in the macro configuration UI.
  • XWiki 16.10.6+, 17.3.0+ Parameter advanced (optional): Indicates whether the parameter should be emphasized or not in the macro configuration UI.
  • XWiki 16.10.6+, 17.3.0+ Parameter deprecated (optional): Indicates if the parameter is deprecated and shouldn't longer be used.
  • XWiki 16.10.6+, 17.3.0+ Parameter feature mandatory (optional): Indicates if the parameter feature is mandatory, i.e. the parameter itself might not be mandatory but if the feature is then at least one of the parameters of the feature should be used.

Now we're going to extend our hello macro with a parameter. We will introduce a parameter named greetUser that will indicate if the greeting message should be tailored for the current user viewing the page. The definition of the parameter is shown below:

Get Connected