Last modified by Simon Urli on 2023/10/10

<
From version < 68.1 >
edited by simba
on 2009/11/16
To version < 69.1 >
edited by simba
on 2009/11/16
>
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -75,6 +75,38 @@
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 +
78 78  1.1 WYSIWYG Access
79 79  
80 80  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:
... ... @@ -83,6 +83,24 @@
83 83  
84 84  {image:macro5.png}
85 85  
118 +1.1.1 Special code for WYSIWYG edit mode
119 +
120 +#warning("needs more explanation or a example.")
121 +
122 +Even in edit mode, the WYSIWYG editor will execute the macro and feed the result back into the document. So for a macro to work correctly in the WYSIWYG editor, a macro may need a special safeguard:
123 +
124 +{code}
125 +{{velocity output="no"}}
126 +#if($context.action == 'edit')
127 + ## Code for WYSIWYG edit mode.
128 +#else
129 + ## Code for normal display.
130 +#end
131 +{{velocity}}
132 +{code}
133 +
134 +The code for WYSIWYG edit mode is typically a reduced version of the normal code. It shares the appearance but lacks certain interactivity which is not needed and may even cause trouble in the edit mode.
135 +
86 86  1.1 Scripting Tips
87 87  
88 88  Following are few useful hints if you plan to do advanced scripting inside your wiki macros:

Get Connected