Changes for page Writing XWiki Rendering Macros in wiki pages
Last modified by Simon Urli on 2023/10/10
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki.s urli1 +XWiki.VincentMassol - Content
-
... ... @@ -41,7 +41,7 @@ 41 41 * **Default category**: Default category under which this macro should be listed 42 42 * **Supports inline mode**: Whether the macro can be used in an inline context or not 43 43 * **Macro Content availability**: {{warning}}before 11.5RC1 this was called **Macro Content Type**{{/warning}} whether this macro should support a body or not 44 -* **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}}java.util.List<java.lang.String>{{/code}}. Leaving the field blank is equivalent to ##UNKWOWN## value. 44 +* **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. 45 45 * **Content description**: A short description about the macro's content to be displayed on the WYSIWYG editor 46 46 * **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) 47 47 * **Asynchronous rendering**: {{info}}Since 10.10{{/info}} Enabled or disable asynchronous rendering of the panel. Disabled by default. ... ... @@ -56,7 +56,7 @@ 56 56 57 57 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: 58 58 59 -{{code}} 59 +{{code language='none'}} 60 60 {{hello/}} 61 61 {{/code}} 62 62 ... ... @@ -67,7 +67,7 @@ 67 67 Starting with {{code}}XWiki 11.4RC1{{/code}} there are two ways to insert the content of the wiki macro. 68 68 69 69 * The easiest way is to use a dedicated macro in the body of the wikimacro:((( 70 -{{code language= "none"}}70 +{{code language='none'}} 71 71 {{wikimacrocontent/}} 72 72 {{/code}} 73 73 ... ... @@ -74,7 +74,7 @@ 74 74 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]]. 75 75 ))) 76 76 * 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:((( 77 -{{code language= "none"}}77 +{{code language='none'}} 78 78 {{velocity}}$wikimacro.content{{/velocity}} 79 79 {{/code}} 80 80 ))) ... ... @@ -90,6 +90,7 @@ 90 90 * Parameter mandatory: Indicates if this particular parameter is mandatory, wiki macro will fail to execute if a mandatory parameter is missing 91 91 * Parameter default value (optional): {{info}}Since 10.10{{/info}} The default value of the parameter when it's empty 92 92 * Parameter type (optional): {{info}}Since 10.10{{/info}} Indicates to which Java type the parameter value (defined as a String) must be converted to (##java.lang.String## by default). 93 +** Example 1: ##java.awt.Color##. Will provie a Color Picker when the macro is edited in the WYSIWYG editor. 93 93 94 94 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: 95 95 ... ... @@ -97,7 +97,7 @@ 97 97 98 98 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: 99 99 100 -{{code}} 101 +{{code language='none'}} 101 101 {{velocity}} 102 102 #if ($wikimacro.parameters.greetUser && "XWiki.XWikiGuest" != "$xcontext.user") 103 103 Hello $xwiki.user.email! ... ... @@ -110,17 +110,17 @@ 110 110 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. 111 111 112 112 Since {{info}}11.5RC1{{/info}}, it is also possible to display the content of a macro parameter by using a dedicated macro: 113 -{{code language= "none"}}Hello {{wikimacroparameter name="greeUsers" /}}{{/code}}114 +{{code language='none'}}Hello {{wikimacroparameter name="greeUsers" /}}{{/code}} 114 114 115 115 Finally, we can test our new version of **hello macro** with the following invocation: 116 116 117 -{{code language= "none"}}118 +{{code language='none'}} 118 118 {{hello greetUser="true"/}} 119 119 {{/code}} 120 120 121 121 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: 122 122 123 -{{code language= "none"}}124 +{{code language='none'}} 124 124 {{velocity}} 125 125 #set ($greet = true) 126 126 {{hello greetUser="$greet"/}} ... ... @@ -131,7 +131,7 @@ 131 131 132 132 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): 133 133 134 -{{code}} 135 +{{code language='properties'}} 135 135 rendering.macro.<macro id>.name=Name of the macro, displayed in the macros list in the macros wizard 136 136 rendering.macro.<macro id>.description=Description of the macro, displayed as a help in the macros list in the macros wizard 137 137 ... ... @@ -143,7 +143,7 @@ 143 143 144 144 In our example, french translations would be something like this: 145 145 146 -{{code}} 147 +{{code language='properties'}} 147 147 rendering.macro.hello.name=Macro pour dire bonjour 148 148 rendering.macro.hello.description=Ceci est une macro qui va dire "Bonjour" a l'utilisateur 149 149 rendering.macro.hello.parameter.greetUser.name=Personnaliser le message ... ... @@ -162,7 +162,7 @@ 162 162 163 163 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: 164 164 165 -{{code language= "velocity"}}166 +{{code language='velocity'}} 166 166 {{velocity}} 167 167 #if("$xcontext.action" != "edit") 168 168 {{html}} ... ... @@ -180,7 +180,7 @@ 180 180 181 181 == WYSIWYG editing of macro content or parameter == 182 182 183 -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.184 +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. 184 184 Note that this is currently only possible if you specified that the macro content (or parameter) type is ##WIKI## type. 185 185 186 186 {{info}} ... ... @@ -193,7 +193,7 @@ 193 193 194 194 * See all bindings in [[the reference documentation page>>doc:extensions:Extension.WikiMacroStore.WebHome||anchor="HBindings"]]. 195 195 * 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:((( 196 -{{code language= "groovy"}}197 +{{code language='groovy'}} 197 197 {{groovy}} 198 198 import java.util.Collections; 199 199 import org.xwiki.rendering.listener.Link; ... ... @@ -213,7 +213,7 @@ 213 213 {{/code}} 214 214 ))) 215 215 * 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:((( 216 -{{code language= "velocity"}}217 +{{code language='velocity'}} 217 217 {{velocity output="no"}} 218 218 ## get the macro content in a velocity string 219 219 #set($wikiresult = $wikimacro.content) ... ... @@ -235,7 +235,7 @@ 235 235 236 236 There is a common pitfall for using optional paramters. The following macro code contains a not so obvious bug: 237 237 238 -{{code languege= "velocity"}}239 +{{code languege='velocity'}} 239 239 {{velocity}} 240 240 #set($greetUser=$xcontext.macro.params.greetUser) 241 241 #if ("true" == $greetUser && "XWiki.XWikiGuest" != "$xcontext.user" ) ... ... @@ -248,7 +248,7 @@ 248 248 249 249 If we invoke it twice in a row: 250 250 251 -{{code}} 252 +{{code language='none'}} 252 252 {{hello greetUser="true" /}} 253 253 {{hello /}} 254 254 {{/code}} ... ... @@ -260,6 +260,6 @@ 260 260 261 261 So in order to get around it, you can use: 262 262 263 -{{code}} 264 +{{code language='none'}} 264 264 #set($greetUser="$!xcontext.macro.params.greetUser") 265 265 {{/code}}