Changes for page Writing XWiki Rendering Macros in wiki pages
Last modified by Clément Desableau on 2023/06/01
Change comment:
Document converted from syntax xwiki/1.0 to syntax xwiki/2.1
Summary
-
Page properties (3 modified, 0 added, 0 removed)
-
Objects (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. dgervalle1 +XWiki.ManuelSmeria - Syntax
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki 1.01 +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.1Prerequisites11 +== Prerequisites == 9 9 10 10 * Wiki macros are only available on XWiki Enterprise 2.0M2 and later versions 11 11 12 - 1.1Macro Visibility and Rights15 +== 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.1Using protected API in wiki macros29 +=== 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.1Hello Macro33 +== 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.1Definition37 +=== 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.1Parameters62 +=== 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.1WYSIWYG Access84 +== 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 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: 103 103 104 -{code} 96 +{{code}} 105 105 {{velocity output="no"}} 106 106 #if("$xcontext.action" != "edit") 107 107 #set($ok = $xwiki.jsx.use("My.Extension")) ... ... @@ -109,23 +109,20 @@ 109 109 ## 110 110 ## Rest of the code. 111 111 {{velocity}} 112 -{code} 104 +{{/code}} 113 113 114 -Check for example the [Lightbox Macro code>extensions:Extension.Lightbox Macro]. 106 +Check for example the [[Lightbox Macro code>>extensions:Extension.Lightbox Macro]]. 115 115 116 - 1.1Scripting Tips108 +== Scripting Tips == 117 117 118 118 Following are few useful hints if you plan to do advanced scripting inside your wiki macros: 119 119 120 -* Access parameters: Use the context object (Ex. \$xcontext.macro.params.param1) 121 - 122 -* Access macro body (if your macro defines one): Use the context object (Ex. \$xcontext.macro.content) 123 - 124 -* 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) 125 - 112 +* Access parameters: Use the context object (Ex. $xcontext.macro.params.param1) 113 +* Access macro body (if your macro defines one): Use the context object (Ex. $xcontext.macro.content) 114 +* 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) 126 126 * 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: 127 127 128 -{code} 117 +{{code}} 129 129 {{groovy}} 130 130 import java.util.Collections; 131 131 import org.xwiki.rendering.listener.Link; ... ... @@ -142,13 +142,13 @@ 142 142 {{/groovy}} 143 143 144 144 This text will not appear in the result. 145 -{code} 134 +{{/code}} 146 146 147 -* 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:136 +* 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: 148 148 149 -{code} 138 +{{code}} 150 150 {{velocity output="no"}} 151 - 140 +## get the macro content in a velocity string 152 152 #set($wikiresult = $xcontext.macro.content) 153 153 ## Add a wrapping div as a sample of the action of this macro 154 154 #set($wikiresult = "(% class='newstyle' %)((($wikiresult)))") ... ... @@ -155,16 +155,17 @@ 155 155 ## parse the string and return the resulting blocks 156 156 #set($xcontext.macro.result = $services.rendering.parse($wikiresult, $xwiki.getCurrentContentSyntaxId()).getChildren()) 157 157 {{/velocity}} 158 -{code} 159 -1.1 Troubleshooting 147 +{{/code}} 160 160 161 - 1.1.1A Pitfallof OptionalParameters149 +== Troubleshooting == 162 162 163 - #info("Thispitfallhas beenfixedinXWiki2.2")151 +=== A Pitfall of Optional Parameters === 164 164 153 +{{info}}This pitfall has been fixed in XWiki 2.2{{/info}} 154 +<p/> 165 165 There is a common pitfall for using optional paramters. The following macro code contains a not so obvious bug: 166 166 167 -{code} 157 +{{code}} 168 168 {{velocity}} 169 169 #set($greetUser=$xcontext.macro.params.greetUser) 170 170 #if ("true" == $greetUser && "XWiki.XWikiGuest" != "$xcontext.user" ) ... ... @@ -173,23 +173,23 @@ 173 173 Hello world! 174 174 #end 175 175 <img src="$image" width="$width" /> 176 -{code} 166 +{{/code}} 177 177 178 178 If we invoke it twice in a row: 179 179 180 -{code} 170 +{{code}} 181 181 {{hello greetUser="true" /}} 182 182 {{hello /}} 183 -{code} 173 +{{/code}} 184 184 185 185 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: 176 + 186 186 * Macro parameters are implemented as global parameters. So, they remains the same across multiple macro invocations. 187 187 * If $xcontext.macro.params.greetUser contains "null", it will not be assigned to $greetUser. This is different from C/C++ or Java. 179 +{{/html}} 180 +{{/velocity}} 188 188 189 189 So in order to get around it, you can use: 190 190 191 -{code} 192 -#set($greetUser="$!xcontext.macro.params.greetUser") 193 -{code} 194 - 195 - 184 +{{code}}#set($greetUser="$!xcontext.macro.params.greetUser"){{/code}} 185 +{{/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.