Hide last authors
Manuel Smeria 85.2 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
Manuel Smeria 85.1 4
Manuel Smeria 85.2 5 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!
Asiri Rathnayake 4.1 6
Vincent Massol 94.3 7 This page is a tutorial but you can also access the [[reference documentation for the Wiki Macro feature>>doc:extensions:Extension.WikiMacroStore.WebHome]].
Vincent Massol 92.1 8
Manuel Smeria 85.2 9 = Hello Macro =
Asiri Rathnayake 5.1 10
Asiri Rathnayake 6.1 11 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.
Asiri Rathnayake 5.1 12
Manuel Smeria 85.2 13 == Definition ==
Asiri Rathnayake 5.1 14
Valdis Vitolins 89.1 15 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##.
Asiri Rathnayake 5.1 16
Thomas Mortagne 90.1 17 {{warning}}
18 There can be only one object of type ##XWiki.WikiMacroClass## per wiki page (if you add more only the first will be used).
19 {{/warning}}
Valdis Vitolins 89.1 20
21 This class contains the following fields:
Thomas Mortagne 98.1 23 * **Macro id**: Id of the macro to be used by users when invoking your macro from wiki code
24 * **Macro name**: Name of the macro to be displayed on the wysiwyg editor
25 * **Macro description**: A short description of the macro to be displayed on the WYSIWYG editor
26 * **Default category**: Default category under which this macro should be listed
27 * **Supports inline mode**: Whether the macro can be used in an inline context or not
Simon Urli 102.1 28 * **Macro Content availability**: {{warning}}before 11.5RC1 this was called **Macro Content Type**{{/warning}} whether this macro should support a body or not
Thomas Mortagne 118.1 29 * **Macro content type**: {{warning}}this field has been renamed **Macro Content Availability** since 11.5RC1{{/warning}} 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 {{code language="java"}}java.util.List<java.lang.String>{{/code}}. Leaving the field blank is equivalent to ##UNKWOWN## value.
Thomas Mortagne 98.1 30 * **Content description**: A short description about the macro's content to be displayed on the WYSIWYG editor
31 * **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)
Vincent Massol 118.2 32 * {{version since="13.1RC1"}}**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.{{/version}}
33 * {{version since="10.10"}}**Asynchronous rendering**: Enabled or disable asynchronous rendering of the panel. Disabled by default.{{/version}}
34 * {{version since="10.10"}}**Cached**: Indicate if the result of the execution of the element should be cached. Disabled by default.{{/version}}
35 * {{version since="10.10"}}**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.{{/version}}
Manuel Smeria 85.2 36
Asiri Rathnayake 24.1 37 Now we can define our hello macro as shown below:
Manuel Smeria 85.2 38
Vincent Massol 119.2 39 {{image reference="macro1.png"/}}
Asiri Rathnayake 7.1 40
Manuel Smeria 85.2 41 == Invocation ==
Asiri Rathnayake 10.1 42
Manuel Smeria 85.2 43 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:
Asiri Rathnayake 10.1 44
Thomas Mortagne 118.1 45 {{code language="none"}}
Valdis Vitolins 86.1 46 {{hello/}}
47 {{/code}}
Asiri Rathnayake 13.1 48
Asiri Rathnayake 16.1 49 And if you view the result it would say "Hello World!" (of course).
Asiri Rathnayake 13.1 50
Valdis Vitolins 86.1 51 == Content ==
Thomas Mortagne 118.1 53 {{version since="11.4RC1"}}
Vincent Massol 117.3 54 There are two ways to insert the content of the wiki macro.
Simon Urli 102.1 55
Vincent Massol 101.1 56 * The easiest way is to use a dedicated macro in the body of the wikimacro:(((
Vincent Massol 117.2 57 {{code language='none'}}
Simon Urli 99.1 58 {{wikimacrocontent/}}
59 {{/code}}
61 Note that by default this makes the content of the macro directly editable in [[the WYSIWYG editor>>https://extensions.xwiki.org/xwiki/bin/view/Extension/CKEditor%20Integration/#HWikiMacros]].
Vincent Massol 101.1 62 )))
Simon Urli 111.1 63 * Another way to manipulate the content is to use the wikimacro binding. For example, when using Velocity, you can write the following script in the macro body:(((
Vincent Massol 117.2 64 {{code language='none'}}
Simon Urli 111.1 65 {{velocity}}$wikimacro.content{{/velocity}}
Valdis Vitolins 87.1 66 {{/code}}
Vincent Massol 101.1 67 )))
Vincent Massol 117.3 68 {{/version}}
Valdis Vitolins 87.1 69
Vincent Massol 117.4 70 For more details, see the [[Scripting Tips section below>>||anchor="HScriptingTips"]].
Vincent Massol 87.2 71
Manuel Smeria 85.2 72 == Parameters ==
Asiri Rathnayake 38.1 73
Manuel Smeria 85.2 74 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:
Asiri Rathnayake 42.1 75
Asiri Rathnayake 43.1 76 * Parameter name: Name of the parameter, users will refer this name when invoking your macro with parameters
Vincent Massol 98.2 77 * Parameter description (optional): A short description of the parameter, this description will be made available on the WYSIWYG editor
Asiri Rathnayake 43.1 78 * Parameter mandatory: Indicates if this particular parameter is mandatory, wiki macro will fail to execute if a mandatory parameter is missing
Vincent Massol 118.2 79 * {{version since="10.10"}}Parameter default value (optional): The default value of the parameter when it's empty{{/version}}
80 * {{version since="10.10"}}Parameter type (optional): Indicates to which Java type the parameter value (defined as a String) must be converted to (##java.lang.String## by default).{{/version}}
slauriere 119.3 81 ** Example 1: ##java.awt.Color##. Will provide a Color Picker when the macro is edited in the WYSIWYG editor.
Manuel Smeria 85.2 82
83 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:
Manuel Smeria 85.1 85 [[image:macro3.png]]
Asiri Rathnayake 42.1 86
Manuel Smeria 85.2 87 A macro parameter defined this way can be accessed from any scripting language within the macro code. For example, we are going to utilize our //greetUser// parameter within **hello macro** as shown below:
Thomas Mortagne 118.1 89 {{code language="none"}}
Simon Urli 113.1 90 {{velocity}}
91 #if ($wikimacro.parameters.greetUser && "XWiki.XWikiGuest" != "$xcontext.user")
92 Hello $xwiki.user.email!
93 #else
94 Hello world!
95 #end
96 {{/velocity}}
97 {{/code}}
Asiri Rathnayake 44.1 98
Simon Urli 111.1 99 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 **$wikimacro.parameters.greetUser**. We plan to introduce some form of direct parameter binding in near future.
Asiri Rathnayake 38.1 100
Simon Urli 103.1 101 Since {{info}}11.5RC1{{/info}}, it is also possible to display the content of a macro parameter by using a dedicated macro:
Thomas Mortagne 118.1 102 {{code language="none"}}Hello {{wikimacroparameter name="greeUsers" /}}{{/code}}
Simon Urli 103.1 103
Manuel Smeria 85.2 104 Finally, we can test our new version of **hello macro** with the following invocation:
Asiri Rathnayake 42.1 105
Thomas Mortagne 118.1 106 {{code language="none"}}
Valdis Vitolins 86.1 107 {{hello greetUser="true"/}}
108 {{/code}}
Asiri Rathnayake 14.1 109
Robert 94.1 110 If you want to call the new version of the **hello macro** with a parameter from a variable you will need to wrap the call in a velocity macro like this:
Thomas Mortagne 118.1 112 {{code language="none"}}
Robert 94.1 113 {{velocity}}
114 #set ($greet = true)
115 {{hello greetUser="$greet"/}}
116 {{/velocity}}
Simon Urli 104.1 117 {{/code}}
Robert 94.1 118
Pascal Bastien 91.2 119 == Translations ==
Anca Luca 91.1 120
Vincent Massol 93.3 121 When your macro is ready, you might want to provide the description of the macro and its parameters in different languages. For that, you need to create a set of translation keys and values (as described [[here>>platform:DevGuide.InternationalizingApplications]]) and then just use the following convention for the keys you add in this storage (no modification is needed on the macro itself, the association of the translations to the macro is done based on a convention of the form of the translation keys):
Anca Luca 91.1 122
Thomas Mortagne 118.1 123 {{code language="properties"}}
Anca Luca 91.1 124 rendering.macro.<macro id>.name=Name of the macro, displayed in the macros list in the macros wizard
125 rendering.macro.<macro id>.description=Description of the macro, displayed as a help in the macros list in the macros wizard
127 rendering.macro.<macro id>.parameter.<parameter name>.name=Name of the macro parameter, to be displayed in the form for the macro settings in the macros wizard
128 rendering.macro.<macro id>.parameter.<parameter name>.description=Description of the macro parameter, to be displayed as a help in the form for the macro settings in the macros wizard
129 {{/code}}
131 Don't forget to make sure that the visibility of the translations is the same as the visibility of the macro, so that anywhere you use the macro you also have the translations.
133 In our example, french translations would be something like this:
Thomas Mortagne 118.1 135 {{code language="properties"}}
Anca Luca 91.1 136 rendering.macro.hello.name=Macro pour dire bonjour
137 rendering.macro.hello.description=Ceci est une macro qui va dire "Bonjour" a l'utilisateur
138 rendering.macro.hello.parameter.greetUser.name=Personnaliser le message
Pascal Bastien 91.2 139 rendering.macro.hello.parameter.greetUser.description=Personnaliser le message pour l'utilisateur courant en train de visualiser la page. Les valeurs possibles sont "true" (oui) et "false" (non).
Anca Luca 91.1 140 {{/code}}
Vincent Massol 117.5 142 = Macro Visibility and Rights =
144 There are 3 levels of visibility for a macro:
146 * ##Global##
147 ** on main wiki (or {{info}}before XWiki 10.4RC1{{/info}}) the macro will be available in all the pages of all the (sub)wikis. Requires the macro author to have **Programming Rights**
148 ** on subwiki {{info}}in 10.4RC1+{{/info}} synonym of ##Current Wiki## visibility
149 * ##Current Wiki##, which means that the macro will be available in all the pages of the wiki the macro is in. Requires the macro author to have **Admin Rights**
150 * ##Current User##, which means that the macro will only be available to the user who is its author. No special rights required.
152 == Using protected API in wiki macros ==
154 Also, if the macro needs to use [[protected API>>platform:DevGuide.Scripting||anchor="HXWikiCoreAccess"]], the author of the macro will need to have programming rights. Note that the macro will always be executed with the rights of its author, and not with the rights of the author of the calling document (the document using the macro). Specifically, if the macro uses protected API, only the macro author needs to have programming rights, not all the authors of the documents that call this macro.
156 = Bindings =
158 See all availalbe bindings in [[the reference documentation page>>doc:extensions:Extension.WikiMacroStore.WebHome||anchor="HBindings"]].
Manuel Smeria 85.2 160 = WYSIWYG Access =
Manuel Smeria 85.1 162 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:
Manuel Smeria 85.2 163
Simon Urli 111.1 164 [[image:macro2.png]]
Manuel Smeria 85.2 165
Simon Urli 113.1 166 [[image:macro4.png]]
Asiri Rathnayake 15.1 167
Manuel Smeria 85.2 168 == Special code for WYSIWYG edit mode ==
Asiri Rathnayake 16.1 169
Denis Gervalle 84.1 170 Even in edit mode, the WYSIWYG editor will execute the macro and feed the result back into the document. If your macro use some JSX, these will not be loaded. But, if your macro produce some Javascript that use those JSX or 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:
simba 69.1 171
Thomas Mortagne 118.1 172 {{code language="velocity"}}
Denis Gervalle 84.1 173 {{velocity}}
Vincent Massol 77.1 174 #if("$xcontext.action" != "edit")
Denis Gervalle 84.1 175 {{html}}
Denis Gervalle 85.3 176 <script type="text/javascript">
Denis Gervalle 84.1 177 //<![CDATA[
178 ... some javascript ...
179 // ]]>
180 </script>
181 {{/html}}
simba 69.1 182 #end
JeromeVelociter 70.1 183 ##
184 ## Rest of the code.
Thomas Mortagne 90.1 185 {{/velocity}}
Manuel Smeria 85.1 186 {{/code}}
simba 69.1 187
Simon Urli 116.1 188 == WYSIWYG editing of macro content or parameter ==
Thomas Mortagne 118.1 190 As specified above you can use the dedicated macros {{code language="none"}}{{wikimacrocontent/}}{{/code}} and {{code language="none"}}{{wikimacroparameter name="foo"/}}{{/code}} to allow the users of your macro to be able to edit the values of the macro directly in the WYSIWYG editor once the macro is inserted.
Simon Urli 116.1 191 Note that this is currently only possible if you specified that the macro content (or parameter) type is ##WIKI## type.
193 {{info}}
194 There is also a known limitation related to CKEditor that prevents editing directly those macros if they are not used as "standalone" macro. Details can be found on the related issue: https://jira.xwiki.org/browse/CKEDITOR-248.
195 {{/info}}
Manuel Smeria 85.2 197 = Scripting Tips =
Asiri Rathnayake 31.1 198
Manuel Smeria 85.2 199 Following are a few useful hints if you plan to do advanced scripting inside your wiki macros:
Asiri Rathnayake 33.1 200
Valdis Vitolins 86.1 201 * Since 2.4M1, it's possible to directly return the desired list of rendering blocks without having to render them first to let them be parsed back by the macro transformation. The benefits are that it could be a lots quicker and most of all it means supporting syntax which does not provide any renderer. It also makes it possible to generate some XDOM which is impossible to write in any some syntax. For example the following wiki macro is generating a LinkBlock targeting a relative URL:(((
Thomas Mortagne 118.1 202 {{code language="groovy"}}
Thomas Mortagne 74.1 203 {{groovy}}
204 import java.util.Collections;
205 import org.xwiki.rendering.listener.Link;
206 import org.xwiki.rendering.block.WordBlock;
207 import org.xwiki.rendering.block.LinkBlock;
209 ref link = new Link();
210 link.setReference("/xwiki/edit/Main/WebHome");
211 link.setType(LinkType.URI);
213 ref linkBlock = new LinkBlock(Collections.singletonList(new WordBlock("Edit home page"))), link, false);
Simon Urli 111.1 215 wikimacro.result = Collections.singletonList(linkBlock)
Thomas Mortagne 74.1 216 {{/groovy}}
218 This text will not appear in the result.
Manuel Smeria 85.1 219 {{/code}}
Manuel Smeria 85.2 220 )))
Simon Urli 111.1 221 * If you are using ##$wikimacro.content## in your velocity macro, that content will not be able to support scripting, since nested scripting is not supported. To workaround that limitation, thanks to the above, you may do the parsing yourself using the rendering service. Here is a small sample:(((
Thomas Mortagne 118.1 222 {{code language="velocity"}}
Denis Gervalle 81.1 223 {{velocity output="no"}}
Denis Gervalle 81.3 224 ## get the macro content in a velocity string
Simon Urli 111.1 225 #set($wikiresult = $wikimacro.content)
Denis Gervalle 81.2 226 ## Add a wrapping div as a sample of the action of this macro
227 #set($wikiresult = "(% class='newstyle' %)((($wikiresult)))")
228 ## parse the string and return the resulting blocks
Simon Urli 111.1 229 #set($wikimacro.result = $services.rendering.parse($wikiresult, $xwiki.getCurrentContentSyntaxId()).getChildren())
Denis Gervalle 81.1 230 {{/velocity}}
Manuel Smeria 85.1 231 {{/code}}
Manuel Smeria 85.2 232 )))
Valdis Vitolins 86.1 233
Manuel Smeria 85.2 234 = Troubleshooting =
Denis Gervalle 81.3 235
Manuel Smeria 85.2 236 == A Pitfall of Optional Parameters ==
Vincent Massol 72.1 237
Manuel Smeria 85.2 238 {{info}}
239 This pitfall has been fixed in XWiki 2.2
240 {{/info}}
Vincent Massol 72.1 241
242 There is a common pitfall for using optional paramters. The following macro code contains a not so obvious bug:
Thomas Mortagne 118.1 244 {{code languege="velocity"}}
Vincent Massol 72.1 245 {{velocity}}
Vincent Massol 77.1 246 #set($greetUser=$xcontext.macro.params.greetUser)
Vincent Massol 72.1 247 #if ("true" == $greetUser && "XWiki.XWikiGuest" != "$xcontext.user" )
248 Hello $xwiki.user.email!
249 #else
250 Hello world!
251 #end
252 <img src="$image" width="$width" />
Manuel Smeria 85.1 253 {{/code}}
Vincent Massol 72.1 254
255 If we invoke it twice in a row:
Thomas Mortagne 118.1 257 {{code language="none"}}
Vincent Massol 72.1 258 {{hello greetUser="true" /}}
259 {{hello /}}
Manuel Smeria 85.1 260 {{/code}}
Vincent Massol 72.1 261
262 The second invocation will not print "Hello World!" as we'd expect. But it will print the same result as the first invocation. The reasons are:
Manuel Smeria 85.1 263
Manuel Smeria 85.2 264 * Macro parameters are implemented as global parameters. So, they remain the same across multiple macro invocations.
265 * If ##$xcontext.macro.params.greetUser## contains "null", it will not be assigned to ##$greetUser##. This is different from C/C++ or Java.
Vincent Massol 72.1 266
267 So in order to get around it, you can use:
Thomas Mortagne 118.1 269 {{code language="none"}}
Manuel Smeria 85.2 270 #set($greetUser="$!xcontext.macro.params.greetUser")
271 {{/code}}

Get Connected