<
From version < 85.1 >
edited by Manuel Smeria
on 2013/02/20
To version < 85.2 >
edited by Manuel Smeria
on 2013/02/20
>
Change comment: minor fixes

Summary

Details

Page properties
Content
... ... @@ -1,25 +1,17 @@
1 -{{velocity filter="none"}}
2 -{{html clean="false" wiki="true"}}
3 -#startfloatingbox()
4 -**Contents**
1 +{{box cssClass="floatinginfobox" title="**Contents**"}}
2 +{{toc/}}
3 +{{/box}}
5 5  
6 -{{toc start="2" depth="3" numbered=""/}}
7 -#endfloatingbox()
8 -<p/>
9 -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!
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!
10 10  
11 -== Prerequisites ==
7 += Macro Visibility and Rights =
12 12  
13 -* Wiki macros are only available on XWiki Enterprise 2.0M2 and later versions
9 +There are 3 levels of visibility for a macro:
14 14  
15 -== Macro Visibility and Rights ==
16 -
17 -There are 3 levels of visibility for a macro:
18 -
19 19  * farm (if we're in a multiwiki environment), meaning that the macro will be available in all the wikis of the farm
20 20  * wiki, which means that the macro will be available in its wiki
21 -* user, which means that the macro will only be available to the which is its author.
22 -<p/>
13 +* user, which means that the macro will only be available to the user which is its author
14 +
23 23  The rights required to create macros are different depending on the visibility we want for our macro:
24 24  
25 25  * the macro author needs to have **programming** rights for a macro available in the whole **farm**
... ... @@ -26,17 +26,17 @@
26 26  * the macro author needs to have **admin** rights for a macro available in its **wiki**
27 27  * no special rights besides the obvious right to edit the page are needed for a macro available only to its author.
28 28  
29 -=== Using protected API in wiki macros ===
21 +== Using protected API in wiki macros ==
30 30  
31 -Also, if the macro needs to use [[protected API>>platform:DevGuide.Scripting#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.
23 +Also, if the macro needs to use [[protected API>>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.
32 32  
33 -== Hello Macro ==
25 += Hello Macro =
34 34  
35 35  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.
36 36  
37 -=== Definition ===
29 +== Definition ==
38 38  
39 -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:
31 +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##. This class contains the following fields:
40 40  
41 41  * Macro id: Id of the macro to be used by users when invoking your macro from wiki code
42 42  * Macro name: Name of the macro to be displayed on the wysiwyg editor
... ... @@ -46,54 +46,54 @@
46 46  * Macro content type: Whether this macro should support a body or not
47 47  * Content description: A short description about the macro's content to be displayed on the WYSIWYG editor
48 48  * 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)
49 -<p/>
41 +
50 50  Now we can define our hello macro as shown below:
51 -<p/>
43 +
52 52  [[image:macro1.png]]
53 53  
54 -=== Invocation ===
46 +== Invocation ==
55 55  
56 -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:
48 +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:
57 57  
58 58  {{code}}{{hello/}}{{/code}}
59 59  
60 60  And if you view the result it would say "Hello World!" (of course).
61 61  
62 -=== Parameters ===
54 +== Parameters ==
63 63  
64 -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:
56 +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:
65 65  
66 66  * Parameter name: Name of the parameter, users will refer this name when invoking your macro with parameters
67 67  * Parameter description: A short description of the parameter, this description will be made available on the WYSIWYG editor
68 68  * Parameter mandatory: Indicates if this particular parameter is mandatory, wiki macro will fail to execute if a mandatory parameter is missing
69 -<p/>
70 -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:
71 -<p/>
61 +
62 +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:
63 +
72 72  [[image:macro3.png]]
73 73  
74 -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:
75 -<p/>
66 +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:
67 +
76 76  [[image:macro4.png]]
77 77  
78 78  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 **$xcontext.macro.params.greetUser**. We plan to introduce some form of direct parameter binding in near future.
79 -<p/>
80 -Finally, we can test our new version of hello macro with the following invocation:
81 81  
82 -{{code}}{{hello greetUser="true"/}}{{/code}}
72 +Finally, we can test our new version of **hello macro** with the following invocation:
83 83  
84 -== WYSIWYG Access ==
74 +{{code language="none"}}{{hello greetUser="true"/}}{{/code}}
85 85  
76 += WYSIWYG Access =
77 +
86 86  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:
87 -<p/>
79 +
88 88  [[image:macro2.png]]
89 -<p/>
81 +
90 90  [[image:macro5.png]]
91 91  
92 -=== Special code for WYSIWYG edit mode ===
84 +== Special code for WYSIWYG edit mode ==
93 93  
94 94  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:
95 95  
96 -{{code}}
88 +{{code language="velocity"}}
97 97  {{velocity}}
98 98  #if("$xcontext.action" != "edit")
99 99  {{html}}
... ... @@ -109,16 +109,16 @@
109 109  {{velocity}}
110 110  {{/code}}
111 111  
112 -== Scripting Tips ==
104 += Scripting Tips =
113 113  
114 -Following are few useful hints if you plan to do advanced scripting inside your wiki macros:
106 +Following are a few useful hints if you plan to do advanced scripting inside your wiki macros:
115 115  
116 -* Access parameters: Use the context object (Ex. $xcontext.macro.params.param1)
117 -* Access macro body (if your macro defines one): Use the context object (Ex. $xcontext.macro.content)
118 -* Access [[MacroTransformationContext>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/transformation/MacroTransformationContext.java]]: Use the context object (Ex. $xcontext.macro.context)
119 -* 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 make 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:
120 -
121 -{{code}}
108 +* Access parameters: Use the context object (Ex. ##$xcontext.macro.params.param1##)
109 +* Access macro body (if your macro defines one): Use the context object (Ex. ##$xcontext.macro.content##)
110 +* Access [[MacroTransformationContext>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-rendering/xwiki-rendering-api/src/main/java/org/xwiki/rendering/transformation/MacroTransformationContext.java]]: Use the context object (Ex. ##$xcontext.macro.context##)
111 +* 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:
112 +(((
113 +{{code language="groovy"}}
122 122  {{groovy}}
123 123  import java.util.Collections;
124 124  import org.xwiki.rendering.listener.Link;
... ... @@ -136,10 +136,10 @@
136 136  
137 137  This text will not appear in the result.
138 138  {{/code}}
139 -
140 -* If you are using $xcontext.macro.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:
141 -
142 -{{code}}
131 +)))
132 +* If you are using ##$xcontext.macro.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:
133 +(((
134 +{{code languege="velocity"}}
143 143  {{velocity output="no"}}
144 144  ## get the macro content in a velocity string
145 145  #set($wikiresult = $xcontext.macro.content)
... ... @@ -149,16 +149,18 @@
149 149  #set($xcontext.macro.result = $services.rendering.parse($wikiresult, $xwiki.getCurrentContentSyntaxId()).getChildren())
150 150  {{/velocity}}
151 151  {{/code}}
144 +)))
145 += Troubleshooting =
152 152  
153 -== Troubleshooting ==
147 +== A Pitfall of Optional Parameters ==
154 154  
155 -=== A Pitfall of Optional Parameters ===
149 +{{info}}
150 +This pitfall has been fixed in XWiki 2.2
151 +{{/info}}
156 156  
157 -{{info}}This pitfall has been fixed in XWiki 2.2{{/info}}
158 -<p/>
159 159  There is a common pitfall for using optional paramters. The following macro code contains a not so obvious bug:
160 160  
161 -{{code}}
155 +{{code languege="velocity"}}
162 162  {{velocity}}
163 163  #set($greetUser=$xcontext.macro.params.greetUser)
164 164  #if ("true" == $greetUser && "XWiki.XWikiGuest" != "$xcontext.user" )
... ... @@ -178,12 +178,11 @@
178 178  
179 179  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:
180 180  
181 -* Macro parameters are implemented as global parameters. So, they remains the same across multiple macro invocations.
182 -* If $xcontext.macro.params.greetUser contains "null", it will not be assigned to $greetUser. This is different from C/C++ or Java.
183 -{{/html}}
184 -{{/velocity}}
175 +* Macro parameters are implemented as global parameters. So, they remain the same across multiple macro invocations.
176 +* If ##$xcontext.macro.params.greetUser## contains "null", it will not be assigned to ##$greetUser##. This is different from C/C++ or Java.
185 185  
186 186  So in order to get around it, you can use:
187 187  
188 -{{code}}#set($greetUser="$!xcontext.macro.params.greetUser"){{/code}}
189 -{{/velocity}}
180 +{{code}}
181 +#set($greetUser="$!xcontext.macro.params.greetUser")
182 +{{/code}}

Get Connected