<
From version < 84.1 >
edited by Denis Gervalle
on 2013/02/20
To version < 85.1 >
edited by Manuel Smeria
on 2013/02/20
>
Change comment: Document converted from syntax xwiki/1.0 to syntax xwiki/2.1

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.dgervalle
1 +XWiki.ManuelSmeria
Syntax
... ... @@ -1,1 +1,1 @@
1 -XWiki 1.0
1 +XWiki 2.1
Content
... ... @@ -1,107 +1,99 @@
1 +{{velocity filter="none"}}
2 +{{html clean="false" wiki="true"}}
1 1  #startfloatingbox()
2 -*Contents*
3 -#toc ("2" "3" "")
4 -#endfloatingbox()
4 +**Contents**
5 5  
6 +{{toc start="2" depth="3" numbered=""/}}
7 +#endfloatingbox()
8 +<p/>
6 6  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!
7 7  
8 -1.1 Prerequisites
11 +== Prerequisites ==
9 9  
10 10  * Wiki macros are only available on XWiki Enterprise 2.0M2 and later versions
11 11  
12 -1.1 Macro Visibility and Rights
15 +== Macro Visibility and Rights ==
13 13  
14 14  There are 3 levels of visibility for a macro:
18 +
15 15  * farm (if we're in a multiwiki environment), meaning that the macro will be available in all the wikis of the farm
16 16  * wiki, which means that the macro will be available in its wiki
17 17  * user, which means that the macro will only be available to the which is its author.
18 -
22 +<p/>
19 19  The rights required to create macros are different depending on the visibility we want for our macro:
20 -* the macro author needs to have *programming* rights for a macro available in the whole *farm*
21 -* the macro author needs to have *admin* rights for a macro available in its *wiki*
24 +
25 +* the macro author needs to have **programming** rights for a macro available in the whole **farm**
26 +* the macro author needs to have **admin** rights for a macro available in its **wiki**
22 22  * no special rights besides the obvious right to edit the page are needed for a macro available only to its author.
23 23  
24 -1.1.1 Using protected API in wiki macros
29 +=== Using protected API in wiki macros ===
25 25  
26 -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.
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.
27 27  
28 -1.1 Hello Macro
33 +== Hello Macro ==
29 29  
30 30  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.
31 31  
32 -1.1.1 Definition
37 +=== Definition ===
33 33  
34 34  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:
35 35  
36 36  * Macro id: Id of the macro to be used by users when invoking your macro from wiki code
37 -
38 38  * Macro name: Name of the macro to be displayed on the wysiwyg editor
39 -
40 40  * Macro description: A short description of the macro to be displayed on the WYSIWYG editor
41 -
42 42  * Default category: Default category under which this macro should be listed
43 -
44 44  * Supports inline mode: Whether the macro can be used in an inline context or not
45 -
46 46  * Macro content type: Whether this macro should support a body or not
47 -
48 48  * Content description: A short description about the macro's content to be displayed on the WYSIWYG editor
49 -
50 50  * 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)
51 -
49 +<p/>
52 52  Now we can define our hello macro as shown below:
51 +<p/>
52 +[[image:macro1.png]]
53 53  
54 -{image:macro1.png}
54 +=== Invocation ===
55 55  
56 -1.1.1 Invocation
57 -
58 58  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:
59 59  
60 -{code}
61 -{{hello/}}
62 -{code}
58 +{{code}}{{hello/}}{{/code}}
63 63  
64 64  And if you view the result it would say "Hello World!" (of course).
65 65  
66 -1.1.1 Parameters
62 +=== Parameters ===
67 67  
68 68  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:
69 69  
70 70  * Parameter name: Name of the parameter, users will refer this name when invoking your macro with parameters
71 -
72 72  * Parameter description: A short description of the parameter, this description will be made available on the WYSIWYG editor
73 -
74 74  * 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/>
72 +[[image:macro3.png]]
75 75  
76 -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:
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/>
76 +[[image:macro4.png]]
77 77  
78 -{image:macro3.png}
79 -
80 -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:
81 -
82 -{image:macro4.png}
83 -
84 -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.
85 -
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/>
86 86  Finally, we can test our new version of hello macro with the following invocation:
87 87  
88 -{code}
89 -{{hello greetUser="true"/}}
90 -{code}
82 +{{code}}{{hello greetUser="true"/}}{{/code}}
91 91  
92 -1.1 WYSIWYG Access
84 +== WYSIWYG Access ==
93 93  
94 -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:
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/>
88 +[[image:macro2.png]]
89 +<p/>
90 +[[image:macro5.png]]
95 95  
96 -{image:macro2.png}
92 +=== Special code for WYSIWYG edit mode ===
97 97  
98 -{image:macro5.png}
99 -
100 -1.1.1 Special code for WYSIWYG edit mode
101 -
102 102  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:
103 103  
104 -{code}
96 +{{code}}
105 105  {{velocity}}
106 106  #if("$xcontext.action" != "edit")
107 107  {{html}}
... ... @@ -115,21 +115,18 @@
115 115  ##
116 116  ## Rest of the code.
117 117  {{velocity}}
118 -{code}
110 +{{/code}}
119 119  
120 -1.1 Scripting Tips
112 +== Scripting Tips ==
121 121  
122 122  Following are few useful hints if you plan to do advanced scripting inside your wiki macros:
123 123  
124 -* Access parameters: Use the context object (Ex. \$xcontext.macro.params.param1)
125 -
126 -* Access macro body (if your macro defines one): Use the context object (Ex. \$xcontext.macro.content)
127 -
128 -* 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)
129 -
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)
130 130  * 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:
131 131  
132 -{code}
121 +{{code}}
133 133  {{groovy}}
134 134  import java.util.Collections;
135 135  import org.xwiki.rendering.listener.Link;
... ... @@ -146,11 +146,11 @@
146 146  {{/groovy}}
147 147  
148 148  This text will not appear in the result.
149 -{code}
138 +{{/code}}
150 150  
151 -* 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:
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:
152 152  
153 -{code}
142 +{{code}}
154 154  {{velocity output="no"}}
155 155  ## get the macro content in a velocity string
156 156  #set($wikiresult = $xcontext.macro.content)
... ... @@ -159,17 +159,17 @@
159 159  ## parse the string and return the resulting blocks
160 160  #set($xcontext.macro.result = $services.rendering.parse($wikiresult, $xwiki.getCurrentContentSyntaxId()).getChildren())
161 161  {{/velocity}}
162 -{code}
151 +{{/code}}
163 163  
164 -1.1 Troubleshooting
153 +== Troubleshooting ==
165 165  
166 -1.1.1 A Pitfall of Optional Parameters
155 +=== A Pitfall of Optional Parameters ===
167 167  
168 -#info("This pitfall has been fixed in XWiki 2.2")
169 -
157 +{{info}}This pitfall has been fixed in XWiki 2.2{{/info}}
158 +<p/>
170 170  There is a common pitfall for using optional paramters. The following macro code contains a not so obvious bug:
171 171  
172 -{code}
161 +{{code}}
173 173  {{velocity}}
174 174  #set($greetUser=$xcontext.macro.params.greetUser)
175 175  #if ("true" == $greetUser && "XWiki.XWikiGuest" != "$xcontext.user" )
... ... @@ -178,22 +178,23 @@
178 178   Hello world!
179 179  #end
180 180  <img src="$image" width="$width" />
181 -{code}
170 +{{/code}}
182 182  
183 183  If we invoke it twice in a row:
184 184  
185 -{code}
174 +{{code}}
186 186  {{hello greetUser="true" /}}
187 187  {{hello /}}
188 -{code}
177 +{{/code}}
189 189  
190 190  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 +
191 191  * Macro parameters are implemented as global parameters. So, they remains the same across multiple macro invocations.
192 192  * 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}}
193 193  
194 194  So in order to get around it, you can use:
195 195  
196 -{code}
197 -#set($greetUser="$!xcontext.macro.params.greetUser")
198 -{code}
199 -
188 +{{code}}#set($greetUser="$!xcontext.macro.params.greetUser"){{/code}}
189 +{{/velocity}}
XWiki.XWikiComments[0]
Comment
... ... @@ -1,3 +1,1 @@
1 -Note
2 -parameters are of type string.
3 -passing other types like a list of string will fail.
1 +Note parameters are of type string. passing other types like a list of string will fail.

Get Connected