Wiki Macro Tutorial

Version 44.1 by Asiri Rathnayake on 2009/07/21
Warning: For security reasons, the document is displayed in restricted mode as it is not the current version. There may be differences and errors due to this.

Failed to execute the [velocity] macro. Cause: [The execution of the [velocity] script macro is not allowed in [xwiki:Documentation.DevGuide.Tutorials.WritingMacros.WikiMacroTutorial.WebHome]. Check the rights of its last author or the parameters if it's rendered from another script.]. Click on this message for details.

Wiki Macro Tutorial

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
  • Wiki macros can only be defined inside the main wiki, though they can be used throughout a farm

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 name: Name of the macro, which will be used by users to invoke your macro
  • Macro description: A short description of the macro, this description will be made available on the WYSIWYG editor (More on this later)
  • Macro content type: Whether this macro should support a body or not
  • Content description: A short description about the macro's content, this description will also be available 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)

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.0 wiki macro, we can invoke our hello macro as below:

{{hello/}}

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

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 allows 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

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 current user viewing the page. The definition of the parameter is show below:

macro3.png

This section is being updated

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:

macro2.png

Scripting Tips

This section is being updated

Get Connected