Last modified by Clément Desableau on 2023/06/01

<
From version < 71.2 >
edited by JeromeVelociter
on 2009/11/16
To version < 72.1 >
edited by Vincent Massol
on 2009/11/16
>
Change comment: Moved pitfalls into a troubleshootin category

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.JeromeVelociter
1 +XWiki.VincentMassol
Content
... ... @@ -75,38 +75,6 @@
75 75  {{hello greetUser="true"/}}
76 76  {code}
77 77  
78 -1.1.1 A Pitfall of Optional Parameters
79 -
80 -There is a common pitfall for using optional paramters. The following macro code contains a not so obvious bug:
81 -
82 -{code}
83 -{{velocity}}
84 -#set($greetUser=$context.macro.params.greetUser)
85 -#if ("true" == $greetUser && "XWiki.XWikiGuest" != "$xcontext.user" )
86 - Hello $xwiki.user.email!
87 -#else
88 - Hello world!
89 -#end
90 -<img src="$image" width="$width" />
91 -{code}
92 -
93 -If we invoke it twice in a row:
94 -
95 -{code}
96 -{{hello greetUser="true" /}}
97 -{{hello /}}
98 -{code}
99 -
100 -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:
101 -* Macro parameters are implemented as global parameters. So, they remains the same across multiple macro invocations.
102 -* If $context.macro.params.greetUser contains "null", it will not be assigned to $greetUser. This is different from C/C++ or Java.
103 -
104 -So in order to get around it, you can use:
105 -
106 -{code}
107 -#set($greetUser="$!context.macro.params.greetUser")
108 -{code}
109 -
110 110  1.1 WYSIWYG Access
111 111  
112 112  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:
... ... @@ -141,3 +141,38 @@
141 141  
142 142  * 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. $context.macro.context)
143 143  
112 +1.1 Troubleshooting
113 +
114 +1.1.1 A Pitfall of Optional Parameters
115 +
116 +There is a common pitfall for using optional paramters. The following macro code contains a not so obvious bug:
117 +
118 +{code}
119 +{{velocity}}
120 +#set($greetUser=$context.macro.params.greetUser)
121 +#if ("true" == $greetUser && "XWiki.XWikiGuest" != "$xcontext.user" )
122 + Hello $xwiki.user.email!
123 +#else
124 + Hello world!
125 +#end
126 +<img src="$image" width="$width" />
127 +{code}
128 +
129 +If we invoke it twice in a row:
130 +
131 +{code}
132 +{{hello greetUser="true" /}}
133 +{{hello /}}
134 +{code}
135 +
136 +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:
137 +* Macro parameters are implemented as global parameters. So, they remains the same across multiple macro invocations.
138 +* If $context.macro.params.greetUser contains "null", it will not be assigned to $greetUser. This is different from C/C++ or Java.
139 +
140 +So in order to get around it, you can use:
141 +
142 +{code}
143 +#set($greetUser="$!context.macro.params.greetUser")
144 +{code}
145 +
146 +

Get Connected