Writing XWiki Rendering Macros in wiki pages
Contents
Wiki macros allow macro authors to develop reusable and distributable macro modules. There is no java code involved; hence no compiling or packaging. Macro author simply needs to create a wiki page according to a particular specification and that's all!
Prerequisites
- Wiki macros are only available on XWiki Enterprise 2.0M2 and later versions
- Wiki macro authors need to have programming rights
- As of XWiki version 2.0, Wiki macros can only be defined inside the main wiki, though they can be used throughout a farm. (In the future, each wiki will have the possibility of defining its own list of macros).
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 it an object of type XWiki.WikiMacroClass. This class contains 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
- Default category: Default category under which this macro should be listed
- Supports inline mode: Whether the macro can be used in an inline context or not
- Macro content type: Whether this macro should support a body or not
- 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)
Invocation
A wiki macro can be invoked just like any other macro is invoked. Since we are writing a xwiki/2.0 wiki macro, we can invoke our hello macro as below:{{hello/}}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: 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
A macro parameter defined this way can be accessed from any scripting language within the macro code. For an example, we are going to utilize our greetUser parameter within hello macro as below:
As you might have realized already, direct binding of parameters is not supported at the moment. That is, you cannot access greetUser parameter with $greetUser. Instead you must use $context.macro.params.greetUser. We plan to introduce some form of direct parameter binding in near future.
Finally, we can test our new version of hello macro with the following invocation:
{{hello greetUser="true"/}}WYSIWYG Access
A wiki macros is treated just like any other rendering macro in the system. As such, the moment you save your wiki macro it will be available to the users through the WYSIWYG editor's Insert Macro dialog box:
Special code for WYSIWYG edit mode
Even in edit mode, the WYSIWYG editor will execute the macro and feed the result back into the document. If your macro includes a JavaScript extension that manipulate the document's DOM (injecting new elements, moving existing elements, removing elements, etc.), you may want to protect the content in WYSIWYG edit mode in order to prevent the performed transformation to get saved. Here is how you can prevent this behavior:{{velocity output="no"}}
#if("$context.action" != "edit")
#set($ok = $xwiki.jsx.use("My.Extension"))
#end
##
## Rest of the code.
{{velocity}}Scripting Tips
Following are few useful hints if you plan to do advanced scripting inside your wiki macros:- Access parameters: Use the context object (Ex. $context.macro.params.param1)
- Access macro body (if your macro defines one): Use the context object (Ex. $context.macro.content)
- Access MacroTransformationContext: Use the context object (Ex. $context.macro.context)
Troubleshooting
A Pitfall of Optional Parameters
There is a common pitfall for using optional paramters. The following macro code contains a not so obvious bug:{{velocity}}
#set($greetUser=$context.macro.params.greetUser)
#if ("true" == $greetUser && "XWiki.XWikiGuest" != "$xcontext.user" )
Hello $xwiki.user.email!
#else
Hello world!
#end
<img src="$image" width="$width" />{{hello greetUser="true" /}}
{{hello /}}- Macro parameters are implemented as global parameters. So, they remains the same across multiple macro invocations.
- If $context.macro.params.greetUser contains "null", it will not be assigned to $greetUser. This is different from C/C++ or Java.
#set($greetUser="$!context.macro.params.greetUser")