Last modified by Simon Urli on 2023/10/10

<
From version < 67.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,10 +83,11 @@
83 83  
84 84  {image:macro5.png}
85 85  
118 +1.1.1 Special code for WYSIWYG edit mode
86 86  
87 -1.1 Use \#if($context.action == 'edit') to guard your code
120 +#warning("needs more explanation or a example.")
88 88  
89 -Even in edit mode, the WYSIWYG editor will execute the macro and feed the result back into the document. (Needs more explanation or a example.) So for a macro to work correctly in the WYSIWYG editor, a macro may need a special safeguard:
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:
90 90  
91 91  {code}
92 92  {{velocity output="no"}}
... ... @@ -96,9 +96,9 @@
96 96   ## Code for normal display.
97 97  #end
98 98  {{velocity}}
99 -{/code}
132 +{code}
100 100  
101 -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 in edit mode.
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.
102 102  
103 103  1.1 Scripting Tips
104 104  

Get Connected