~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/data/System/IfStatements.txt

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%META:TOPICINFO{author="ProjectContributor" date="1231502400" format="1.1" version="1"}%
 
2
---+ IF Statements
 
3
 
 
4
The =%<nop>IF%= construct gives Foswiki the power to include content in topics based on the value of simple expressions.
 
5
 
 
6
%TOC%
 
7
 
 
8
---++ Syntax
 
9
=%<nop>IF{"CONDITION" then="THEN" else="ELSE"}%=
 
10
 
 
11
In the example above, if CONDITION evaluates to TRUE, then THEN will be included in the topic; otherwise ELSE will be included.
 
12
 
 
13
Note that because of the way Foswiki 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 macro.
 
14
 
 
15
The basic syntax of a condition is the same as [[QuerySearch][the syntax used for queries]], with the addition of the following special operators:
 
16
 
 
17
| =context= | True if the current context is set (see below) |
 
18
| =allows= | ='X' allows 'Y'= 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'. |
 
19
| =istopic= | =istopic 'X'= is true if topic 'X' exists |
 
20
| =isweb= | =isweb 'X'= is true if web 'X' exists |
 
21
| =ingroup= | ='X' ingroup 'Y'= is true if user 'X' is in group 'Y'. 'X' can be a login name or a wikiname. |
 
22
| =defined= | True if a [[PreferenceSettings][preference setting]] or url parameter of this name is defined. |
 
23
| =isempty= | True if a [[PreferenceSettings][preference setting]], url parameter or session variable of this name has an empty value. It is equivalent to the expression =(!defined(x) &#124;&#124; $x='')= |
 
24
| =$= | expands a URL parameter or [[Macros][macro]] name. Plugin handlers *are not called*. You can pass a limited subset of parameters to macros by enclosing the parameter string in single quotes; for example, =$ 'MACRO{value}'=. The ='MACRO{value}'= string may *not* contain quotes (' or "). |
 
25
| ={X}= | expands to the value of the =configure= setting {X} - for example, ={ScriptUrlPath}= |
 
26
 
 
27
---+++ Examples
 
28
 
 
29
1. Macro defined or not
 
30
<verbatim>
 
31
%IF{"defined 'WIKINAME'" then="WIKINAME is defined" else="WIKINAME is not defined"}%
 
32
</verbatim>
 
33
 
 
34
2. Compare macro definition
 
35
<verbatim>
 
36
You are %IF{ "$ WIKINAME='WikiGuest' and not defined 'OPEN_DAY'" then="not" }% allowed to
 
37
%IF{ "context view" then="view" else="edit"}% this Foswiki today.
 
38
</verbatim>
 
39
 
 
40
3. URL parameter
 
41
<verbatim>
 
42
%IF{ "defined 'search'" then="Search: $percntURLPARAM{search}$percnt" else="No search passed in"}%
 
43
</verbatim>
 
44
 
 
45
4. Range test on URL parameter
 
46
<verbatim>
 
47
url param t is %IF{ "0 < $ t and $ t < 1000" then="in" else="out of"}% range.
 
48
</verbatim>
 
49
 
 
50
5. Text comparison of URL parameter
 
51
<verbatim>
 
52
%IF{ "$'URLPARAM{scope}'='text'" then="Plain text search" }% 
 
53
</verbatim>
 
54
 
 
55
6. Configuration item set or not
 
56
<verbatim>
 
57
%IF{ "{AntiSpam}{HideUserDetails}" then="User details are hidden" }%
 
58
</verbatim>
 
59
 
 
60
7. Plugin enabled test
 
61
<verbatim>
 
62
TablePlugin is %IF{ "context TablePluginEnabled" then="enabled" else="disabled" }%.
 
63
</verbatim>
 
64
expands to: %BR%
 
65
TablePlugin is %IF{ "context TablePluginEnabled" then="enabled" else="disabled" }%.
 
66
 
 
67
8. Check access permissions
 
68
<verbatim>
 
69
You %IF{"'%TOPIC%' allows 'change'" then="can" else="cannot"}% change this topic.
 
70
You %IF{"'Sandbox.TestTopic' allows 'change'" then="can" else="cannot"}% change Sandbox.TestTopic.
 
71
You %IF{"'Sandbox' allows 'change'" then="can" else="cannot"}% change Sandbox web
 
72
</verbatim>
 
73
expands to: %BR%
 
74
You %IF{"'%TOPIC%' allows 'change'" then="can" else="cannot"}% change this topic.
 
75
You %IF{"'Sandbox.TestTopic' allows 'change'" then="can" else="cannot"}% change Sandbox.TestTopic.
 
76
You %IF{"'Sandbox' allows 'change'" then="can" else="cannot"}% change Sandbox web
 
77
 
 
78
9. Check topic existance
 
79
<verbatim>
 
80
Topic Sandbox.TestTopic %IF{"istopic 'Sandbox.TestTopic'" then="exists" else="does not exist"}%
 
81
Web Sandbox.TestTopic %IF{"isweb 'Sandbox'" then="exists" else="does not exist"}%
 
82
</verbatim>
 
83
expands to: %BR%
 
84
Topic Sandbox.TestTopic %IF{"istopic 'Sandbox.TestTopic'" then="exists" else="does not exist"}%
 
85
Web Sandbox.TestTopic %IF{"isweb 'Sandbox'" then="exists" else="does not exist"}%
 
86
 
 
87
10. Group membership
 
88
<verbatim>
 
89
You %IF{"'%USERNAME%' ingroup 'AdminGroup'" then="are an admin" else="are a normal user"}% 
 
90
</verbatim>
 
91
expands to: %BR%
 
92
You %IF{"'%USERNAME%' ingroup 'AdminGroup'" then="are an admin" else="are a normal user"}% 
 
93
 
 
94
*Configuration items* are defined in [[%SCRIPTURLPATH{"configure"}%][configure]]. You cannot see the value of a configuration item, you can only see if the item is set or not.
 
95
 
 
96
---++ Context identifiers
 
97
Context identifiers are used in Foswiki to label various stages of the rendering process. They are especially useful for [[Skins][skin]] authors to find out where they are in the rendering process. The following context identifiers are available:
 
98
| *id* | *context* |
 
99
| =absolute_urls= | Set if absolute URLs are required |
 
100
| =attach= | in attach script (see CommandAndCGIScripts) |
 
101
| =authenticated= | a user is authenticated |
 
102
| =body_text=  | when the body text is being processed in a view (useful in plugin handlers) |
 
103
| =can_login= | current environment supports login |
 
104
| =changes= | in changes script (see CommandAndCGIScripts) |
 
105
| =command_line= | the running script was run from the command line, and not from CGI |
 
106
| =diff= | in rdiff script (see CommandAndCGIScripts) |
 
107
| =edit= | in edit script (see CommandAndCGIScripts) |
 
108
| =footer_text= | when the footer text is being processed in a view (useful in plugin handlers) |
 
109
| =header_text= | when the header text is being processed in a view (useful in plugin handlers) |
 
110
| =i18n_enabled= | when user interface <nop>I18N support is enabled (i.e., user can choose the language for UI) |
 
111
| =inactive= | if active links such as 'edit' and 'attach' should be disabled |
 
112
| =login= and =logon= | in login / logon script (see CommandAndCGIScripts) |
 
113
| =manage= | in manage script (see CommandAndCGIScripts) |
 
114
| =mirror= | if this is a mirror |
 
115
| =new_topic= | if the topic doesn't already exist |
 
116
| =oops= | in oops script (see CommandAndCGIScripts) |
 
117
| =preview= | in preview script (see CommandAndCGIScripts) |
 
118
| =register= | in register script (see CommandAndCGIScripts) |
 
119
| =rename= | in rename script (see CommandAndCGIScripts) |
 
120
| =resetpasswd= | in resetpasswd script (see CommandAndCGIScripts) |
 
121
| =rss= | if this is an RSS skin rendering |
 
122
| =save= | in save script (see CommandAndCGIScripts) |
 
123
| =search= | in search script (see CommandAndCGIScripts) |
 
124
| =statistics= | in statistics script (see CommandAndCGIScripts) |
 
125
| =textareas_hijacked= | 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. |
 
126
| =upload= | in upload script (see CommandAndCGIScripts) |
 
127
| =view= | in view script (see CommandAndCGIScripts) |
 
128
| =viewfile= | in viewfile script (see CommandAndCGIScripts) |
 
129
| =rest= | in rest script (see CommandAndCGIScripts) |
 
130
| =registration_supported= | registration is supported by the current UserMapper |
 
131
| =registration_enabled= | set if ={Register}{EnableNewUserRegistration}= is on, and registrationis supported |
 
132
| =passwords_modifyable= | set if the password manager support changing the password / email |
 
133
 
 
134
In addition there is a context identifier for each enabled plugin; for example, if =Gallous<nop>BreeksPlugin= is installed *and enabled*, then the context ID =Gallous<nop>BreeksPluginEnabled= will be set. Other extensions may set additional context identifiers.
 
135
 
 
136
The =%<nop>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.
 
137
 
 
138
---++ Query syntax
 
139
 
 
140
Note also that while the [[QuerySearch][query syntax]] 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.
 
141
 
 
142
---+++ Examples
 
143
Display the value of a form field if the topic has form field "Summary":
 
144
<verbatim>
 
145
%FORMFIELD{"%IF{"'%TOPIC%'/Summary" then="Summary"}%"}%
 
146
</verbatim>
 
147
 
 
148
Test if the topic has attachments:
 
149
<verbatim>
 
150
%IF{"'%TOPIC%'/attachments" then="has attachments"}%
 
151
</verbatim>
 
152
 
 
153
Test if the topic has an attachment with 'receipt' or 'Receipt' in the name:
 
154
<verbatim>
 
155
%IF{"'%TOPIC%'/attachments[lc(name)~'*receipt*']" then="has 'receipt' attachment"}%
 
156
</verbatim>
 
157
 
 
158
Test if a topic text contains a certain word:
 
159
<verbatim>
 
160
%IF{"'%SYSTEMWEB%.WebHome'/lc(text)~'*welcome*'" then="contains 'welcome'"}%
 
161
</verbatim>
 
162