none
[openafs-wiki.git] / TWiki / IfStatements.mdwn
1 # <a name="IF Statements"></a> IF Statements
2
3 The `%IF%` construct gives TWiki the power to include content in topics based on the value of simple expressions.
4
5 `%IF{"CONDITION" then="THEN" else="ELSE"}%`
6
7 In the example above, if CONDITION evaluates to TRUE, then THEN will be included in the topic; otherwise ELSE will be included.
8
9 Note that because of the way TWiki evaluates, then whatever is in the THEN and ELSE parameters will already have been expanded by the time the condition is actually evaluated. The standard [[FormatTokens]] can be used in the THEN and ELSE parameters when you need to delay evaluation of (for example) a TWiki variable.
10
11 The basic syntax of a condition is the same as [[the syntax used for queries|Main/QuerySearch]], with the addition of the following special operators:
12
13 <table border="1" cellpadding="0" cellspacing="0">
14   <tr>
15     <td> context </td>
16     <td> True if the current context is set (see below) </td>
17   </tr>
18   <tr>
19     <td> allows </td>
20     <td><code>'X' allows 'Y'</code> is true if web/topic 'X' exists and allows access mode 'Y' for the current user. Web access rights are only checked if there is no topic called 'X'. </td>
21   </tr>
22   <tr>
23     <td> istopic </td>
24     <td><code>istopic 'X'</code> is true if topic 'X' exists </td>
25   </tr>
26   <tr>
27     <td> isweb </td>
28     <td><code>isweb 'X'</code> is true if web 'X' exists </td>
29   </tr>
30   <tr>
31     <td> ingroup </td>
32     <td><code>'X' ingroup 'Y'</code> is true if user 'X' is in group 'Y'. 'X' can be a login name or a wikiname. </td>
33   </tr>
34   <tr>
35     <td> defined </td>
36     <td> True if a preference variable or url parameter of this name is defined. </td>
37   </tr>
38   <tr>
39     <td> $ </td>
40     <td> expands a URL parameter or [[Main/TWikiVariables]] name. Plugin handlers <strong>are not called</strong>. Built-in variables and user-defined preferences are supported. You can pass a limited subset of parameters to TWiki variables by enclosing the variable name in single quotes; for example, <code>$ 'VARIABLE{value}'</code>. The <code>'VARIABLE{value}'</code> string may <strong>not</strong> contain quotes (' or "). </td>
41   </tr>
42   <tr>
43     <td> {X} </td>
44     <td> expands to the value of the configuration variable {X} - for example, <code>{ScriptUrlPath}</code></td>
45   </tr>
46 </table>
47
48 **_Examples:_**
49
50 1. TWiki variable defined or not
51
52     %IF{"defined 'WIKINAME'" then="WIKINAME is defined" else="WIKINAME is not defined"}%
53
54 2. Compare TWiki variable
55
56     You are %IF{ "$ WIKINAME='TWikiGuest' and not defined 'OPEN_DAY'" then="not" }% allowed to
57     %IF{ "context view" then="view" else="edit"}% this TWiki today.
58
59 3. URL parameter
60
61     %IF{ "defined 'search'" then="Search: $percntURLPARAM{search}$percnt" else="No search passed in"}%
62
63 4. Range test on URL parameter
64
65     url param t is %IF{ "0 < $ t and $ t < 1000" then="in" else="out of"}% range.
66
67 5. Text comparison of URL parameter
68
69     %IF{ "$'URLPARAM{scope}'='text'" then="Plain text search" }%
70
71 6. Configuration item set or not
72
73     %IF{ "{AntiSpam}{HideUserDetails}" then="User details are hidden" }%
74
75 7. Plugin enabled test
76
77     TablePlugin is %IF{ "context TablePluginEnabled" then="enabled" else="disabled" }%.
78
79 expands to: %BR% [[TablePlugin]] is enabled.
80
81 8. Check access permissions
82
83     You %IF{"'%TOPIC%' allows 'change'" then="can" else="cannot"}% change this topic.
84     You %IF{"'Sandbox.TestTopic' allows 'change'" then="can" else="cannot"}% change Sandbox.TestTopic.
85     You %IF{"'Sandbox' allows 'change'" then="can" else="cannot"}% change Sandbox web
86
87 expands to: %BR% You can change this topic. You can change [[TestTopic]]. You can change Sandbox web
88
89 9. Check topic existance
90
91     Topic Sandbox.TestTopic %IF{"istopic 'Sandbox.TestTopic'" then="exists" else="does not exist"}%
92     Web Sandbox.TestTopic %IF{"isweb 'Sandbox'" then="exists" else="does not exist"}%
93
94 expands to: %BR% Topic [[TestTopic]] exists Web [[TestTopic]] exists
95
96 10. Group membership
97
98     You %IF{"'%USERNAME%' ingroup 'TWikiAdminGroup'" then="are an admin" else="are a normal user"}%
99
100 expands to: %BR% You are an admin
101
102 **Configuration items** are defined in [configure](http://www.dementia.org/twiki/configure). You cannot see the value of a configuration item, you can only see if the item is set or not.
103
104 **Context identifiers** are used in TWiki to label various stages of the rendering process. They are especially useful for [[skin|Main/TWikiSkins]] authors to find out where they are in the rendering process. The following context identifiers are available:
105
106 <table border="1" cellpadding="0" cellspacing="0">
107   <tr>
108     <th bgcolor="#99CCCC"><strong> id </strong></th>
109     <th bgcolor="#99CCCC"><strong> context </strong></th>
110   </tr>
111   <tr>
112     <td> absolute_urls </td>
113     <td> Set if absolute URLs are required </td>
114   </tr>
115   <tr>
116     <td> attach </td>
117     <td> in attach script (see [[Main/TWikiScripts]]) </td>
118   </tr>
119   <tr>
120     <td> authenticated </td>
121     <td> a user is authenticated </td>
122   </tr>
123   <tr>
124     <td> body_text </td>
125     <td> when the body text is being processed in a view (useful in plugin handlers) </td>
126   </tr>
127   <tr>
128     <td> can_login </td>
129     <td> current environment supports login </td>
130   </tr>
131   <tr>
132     <td> changes </td>
133     <td> in changes script (see [[Main/TWikiScripts]]) </td>
134   </tr>
135   <tr>
136     <td> command_line </td>
137     <td> the running script was run from the command line, and not from CGI </td>
138   </tr>
139   <tr>
140     <td> diff </td>
141     <td> in rdiff script (see [[Main/TWikiScripts]]) </td>
142   </tr>
143   <tr>
144     <td> edit </td>
145     <td> in edit script (see [[Main/TWikiScripts]]) </td>
146   </tr>
147   <tr>
148     <td> footer_text </td>
149     <td> when the footer text is being processed in a view (useful in plugin handlers) </td>
150   </tr>
151   <tr>
152     <td> header_text </td>
153     <td> when the header text is being processed in a view (useful in plugin handlers) </td>
154   </tr>
155   <tr>
156     <td> i18n_enabled </td>
157     <td> when user interface I18N support is enabled (i.e., user can choose the language for UI) </td>
158   </tr>
159   <tr>
160     <td> inactive </td>
161     <td> if active links such as 'edit' and 'attach' should be disabled </td>
162   </tr>
163   <tr>
164     <td> login &amp; logon </td>
165     <td> in login / logon script (see [[Main/TWikiScripts]]) </td>
166   </tr>
167   <tr>
168     <td> manage </td>
169     <td> in manage script (see [[Main/TWikiScripts]]) </td>
170   </tr>
171   <tr>
172     <td> mirror </td>
173     <td> if this is a mirror </td>
174   </tr>
175   <tr>
176     <td> new_topic </td>
177     <td> if the topic doesn't already exist </td>
178   </tr>
179   <tr>
180     <td> oops </td>
181     <td> in oops script (see [[Main/TWikiScripts]]) </td>
182   </tr>
183   <tr>
184     <td> preview </td>
185     <td> in preview script (see [[Main/TWikiScripts]]) </td>
186   </tr>
187   <tr>
188     <td> register </td>
189     <td> in register script (see [[Main/TWikiScripts]]) </td>
190   </tr>
191   <tr>
192     <td> rename </td>
193     <td> in rename script (see [[Main/TWikiScripts]]) </td>
194   </tr>
195   <tr>
196     <td> resetpasswd </td>
197     <td> in resetpasswd script (see [[Main/TWikiScripts]]) </td>
198   </tr>
199   <tr>
200     <td> rss </td>
201     <td> if this is an RSS skin rendering </td>
202   </tr>
203   <tr>
204     <td> save </td>
205     <td> in save script (see [[Main/TWikiScripts]]) </td>
206   </tr>
207   <tr>
208     <td> search </td>
209     <td> in search script (see [[Main/TWikiScripts]]) </td>
210   </tr>
211   <tr>
212     <td> statistics </td>
213     <td> in statistics script (see [[Main/TWikiScripts]]) </td>
214   </tr>
215   <tr>
216     <td> textareas_hijacked </td>
217     <td> provided for use by editors that highjack textareas, and want to signal this fact. This is used by skins, for example, so they can suppress extra controls when textareas have been hijacked. </td>
218   </tr>
219   <tr>
220     <td> upload </td>
221     <td> in upload script (see [[Main/TWikiScripts]]) </td>
222   </tr>
223   <tr>
224     <td> view </td>
225     <td> in view script (see [[Main/TWikiScripts]]) </td>
226   </tr>
227   <tr>
228     <td> viewfile </td>
229     <td> in viewfile script (see [[Main/TWikiScripts]]) </td>
230   </tr>
231   <tr>
232     <td> rest </td>
233     <td> in rest script (see [[Main/TWikiScripts]]) </td>
234   </tr>
235   <tr>
236     <td> registration_supported </td>
237     <td> registration is supported by the current [[Main/UserMapper]]</td>
238   </tr>
239   <tr>
240     <td> registration_enabled </td>
241     <td> set if <code>{Register}{EnableNewUserRegistration}</code> is on, and registrationis supported </td>
242   </tr>
243   <tr>
244     <td> passwords_modifyable </td>
245     <td> set if the password manager support changing the password / email </td>
246   </tr>
247 </table>
248
249 In addition there is a context identifier for each enabled plugin; for example, if `GallousBreeksPlugin` is installed **and enabled**, then the context ID `GallousBreeksPluginEnabled` will be set. Other extensions may set additional context identifiers.
250
251 The `%IF%` statement is deliberately kept simple. In particular, note that there is no way to conditionally execute a Set statement. If you need more sophisticated control over formatting, then consider using the [[SpreadSheetPlugin]].
252
253 Note also that while the [[query syntax|Main/QuerySearch]] can be used to access form fields, there are some contexts in which an IF statement may be used where there is no topic context, or the topic context is not what you expected.