1 # <a name="=IF= Statements"></a> `IF` Statements
3 The `%IF%` construct gives TWiki the power to include content in topics based on the value of simple expressions.
5 `%IF{"CONDITION" then="THEN" else="ELSE"}%`
7 In the example above, if CONDITION evaluates to TRUE, then THEN will be included in the topic; otherwise ELSE will be included.
9 What can be included in the THEN and ELSE parameters is obviously limited by standard TWiki syntax for parameters.
11 The basic syntax of a condition is as follows:
13 expr ::= '(' expr ')' ;
14 expr ::= andexpr | andexpr 'or' expr ;
15 andexpr ::= notexpr | notexpr 'and' andexpr ;
16 notexpr ::= basexpr | 'not' baseexpr ;
17 basexpr ::= atom | uop atom | atom bop basexpr ;
18 uop ::= 'context' | 'defined' | '$' ;
19 bop ::= '=' | '!=' | '>' | '<' | '>=' | '<=' ;
20 atom ::= context identifier, TWiki variable name, single-quoted string, or configuration item
21 <table border="1" cellpadding="0" cellspacing="0">
23 <th bgcolor="#99CCCC" colspan="2"><strong> Operators </strong></th>
27 <td> True if both sides are true </td>
31 <td> True if one or other side is true </td>
35 <td> negate the following expression </td>
39 <td> String comparison </td>
42 <td> <, >, <=, >= </td>
43 <td> Number comparison (there is no explicit numeric =) </td>
47 <td> True if the current context is set (see below) </td>
51 <td> True if a preference variable or url parameter of this name is defined. </td>
55 <td> expands a URL parameter or [[Main/TWikiVariables]]. 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>
61 TWiki variable defined or not
62 %IF{"defined WIKINAME" then="WIKINAME is defined" else="WIKINAME is not defined"}%
64 You are %IF{ "$ WIKINAME='TWikiGuest' and not defined OPEN_DAY" then="not" }% allowed to
65 %IF{ "context view" then="view" else="edit"}% this TWiki today.
68 %IF{ "defined search" then="Search: %URLPARAM{search}%" else="No search passed in"}%
70 Configuration item set or not
71 %IF{ "{AntiSpam}{HideUserDetails}" then="User details are hidden" }%
73 url param t is %IF{ "0 < $ t and $ t < 1000" then="in" else="out of"}% range.
76 %IF{ "$'URLPARAM{scope}'='text'" then="Plain text search" }%
78 **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.
80 **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:
82 <table border="1" cellpadding="0" cellspacing="0">
84 <th bgcolor="#99CCCC"><strong> id </strong></th>
85 <th bgcolor="#99CCCC"><strong> context </strong></th>
88 <td> absolute_urls </td>
89 <td> Set if absolute URLs are required </td>
92 <td> authenticated </td>
93 <td> a user is authenticated </td>
97 <td> when the body text is being processed in a view (useful in plugin handlers) </td>
101 <td> current environment supports login </td>
105 <td> in changes script (see [[Main/TWikiScripts]]) </td>
108 <td> command_line </td>
109 <td> the running script was run from the command line, and not from CGI </td>
113 <td> in rdiff script (see [[Main/TWikiScripts]]) </td>
117 <td> in edit script (see [[Main/TWikiScripts]]) </td>
120 <td> footer_text </td>
121 <td> when the footer text is being processed in a view (useful in plugin handlers) </td>
124 <td> header_text </td>
125 <td> when the header text is being processed in a view (useful in plugin handlers) </td>
128 <td> i18n_enabled </td>
129 <td> when user interface I18N support is enabled (i.e., user can choose the language for UI) </td>
133 <td> if active links such as 'edit' and 'attach' should be disabled </td>
137 <td> if this is a mirror </td>
141 <td> if the topic doesn't already exist </td>
145 <td> in oops script (see [[Main/TWikiScripts]]) </td>
149 <td> in preview script (see [[Main/TWikiScripts]]) </td>
153 <td> if this is an RSS skin rendering </td>
157 <td> in save script (see [[Main/TWikiScripts]]) </td>
161 <td> in search script (see [[Main/TWikiScripts]]) </td>
165 <td> in view script (see [[Main/TWikiScripts]]) </td>
169 <td> in rest script (see [[Main/TWikiScripts]]) </td>
173 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.
175 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]].