data/TWiki/IfStatements.txt
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
child 1 e2915a7cbdfa
permissions -rw-r--r--
RELEASE 4.2.0 freetown
colas@0
     1
%META:TOPICINFO{author="TWikiContributor" date="1183830381" format="1.1" version="6"}%
colas@0
     2
---+ IF Statements
colas@0
     3
colas@0
     4
The =%<nop>IF%= construct gives TWiki the power to include content in topics based on the value of simple expressions.
colas@0
     5
colas@0
     6
=%<nop>IF{"CONDITION" then="THEN" else="ELSE"}%=
colas@0
     7
colas@0
     8
In the example above, if CONDITION evaluates to TRUE, then THEN will be included in the topic; otherwise ELSE will be included.
colas@0
     9
colas@0
    10
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.
colas@0
    11
colas@0
    12
The basic syntax of a condition is the same as [[QuerySearch][the syntax used for queries]], with the addition of the following special operators:
colas@0
    13
colas@0
    14
| context | True if the current context is set (see below) |
colas@0
    15
| 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'. |
colas@0
    16
| istopic | =istopic 'X'= is true if topic 'X' exists |
colas@0
    17
| isweb | =isweb 'X'= is true if web 'X' exists |
colas@0
    18
| ingroup | ='X' ingroup 'Y'= is true if user 'X' is in group 'Y' |
colas@0
    19
| defined | True if a preference variable or url parameter of this name is defined. |
colas@0
    20
| $ | expands a URL parameter or [[TWikiVariables][TWikiVariable]] name. Plugin handlers *are not called*. 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, =$ 'VARIABLE{value}'=. The ='VARIABLE{value}'= string may *not* contain quotes (' or "). |
colas@0
    21
| {X} | expands to the value of the configuration variable {X} - for example, ={ScriptUrlPath}= |
colas@0
    22
colas@0
    23
__Examples:__
colas@0
    24
colas@0
    25
1. TWiki variable defined or not
colas@0
    26
<verbatim>
colas@0
    27
%IF{"defined WIKINAME" then="WIKINAME is defined" else="WIKINAME is not defined"}%
colas@0
    28
</verbatim>
colas@0
    29
colas@0
    30
2. Compare TWiki variable
colas@0
    31
<verbatim>
colas@0
    32
You are %IF{ "$ WIKINAME='TWikiGuest' and not defined OPEN_DAY" then="not" }% allowed to
colas@0
    33
%IF{ "context view" then="view" else="edit"}% this TWiki today.
colas@0
    34
</verbatim>
colas@0
    35
colas@0
    36
3. URL parameter
colas@0
    37
<verbatim>
colas@0
    38
%IF{ "defined search" then="Search: $percntURLPARAM{search}$percnt" else="No search passed in"}%
colas@0
    39
</verbatim>
colas@0
    40
colas@0
    41
4. Range test on URL parameter
colas@0
    42
<verbatim>
colas@0
    43
url param t is %IF{ "0 < $ t and $ t < 1000" then="in" else="out of"}% range.
colas@0
    44
</verbatim>
colas@0
    45
colas@0
    46
5. Text comparison of URL parameter
colas@0
    47
<verbatim>
colas@0
    48
%IF{ "$'URLPARAM{scope}'='text'" then="Plain text search" }% 
colas@0
    49
</verbatim>
colas@0
    50
colas@0
    51
6. Configuration item set or not
colas@0
    52
<verbatim>
colas@0
    53
%IF{ "{AntiSpam}{HideUserDetails}" then="User details are hidden" }%
colas@0
    54
</verbatim>
colas@0
    55
colas@0
    56
7. Plugin enabled test
colas@0
    57
<verbatim>
colas@0
    58
TablePlugin is %IF{ "context TablePluginEnabled" then="enabled" else="disabled" }%.
colas@0
    59
</verbatim>
colas@0
    60
expands to: %BR%
colas@0
    61
TablePlugin is %IF{ "context TablePluginEnabled" then="enabled" else="disabled" }%.
colas@0
    62
colas@0
    63
8. Check access permissions
colas@0
    64
<verbatim>
colas@0
    65
You %IF{"'%TOPIC%' allows 'change'" then="can" else="cannot"}% change this topic.
colas@0
    66
You %IF{"'Sandbox.TestTopic' allows 'change'" then="can" else="cannot"}% change Sandbox.TestTopic.
colas@0
    67
You %IF{"'Sandbox' allows 'change'" then="can" else="cannot"}% change Sandbox web
colas@0
    68
</verbatim>
colas@0
    69
expands to: %BR%
colas@0
    70
You %IF{"'%TOPIC%' allows 'change'" then="can" else="cannot"}% change this topic.
colas@0
    71
You %IF{"'Sandbox.TestTopic' allows 'change'" then="can" else="cannot"}% change Sandbox.TestTopic.
colas@0
    72
You %IF{"'Sandbox' allows 'change'" then="can" else="cannot"}% change Sandbox web
colas@0
    73
colas@0
    74
9. Check topic existance
colas@0
    75
<verbatim>
colas@0
    76
Topic Sandbox.TestTopic %IF{"istopic 'Sandbox.TestTopic'" then="exists" else="does not exist"}%
colas@0
    77
Web Sandbox.TestTopic %IF{"isweb 'Sandbox'" then="exists" else="does not exist"}%
colas@0
    78
</verbatim>
colas@0
    79
expands to: %BR%
colas@0
    80
Topic Sandbox.TestTopic %IF{"istopic 'Sandbox.TestTopic'" then="exists" else="does not exist"}%
colas@0
    81
Web Sandbox.TestTopic %IF{"isweb 'Sandbox'" then="exists" else="does not exist"}%
colas@0
    82
colas@0
    83
10. Group membership
colas@0
    84
<verbatim>
colas@0
    85
You %IF{"'%USERNAME%' ingroup 'TWikiAdminGroup'" then="are an admin" else="are a normal user"}% 
colas@0
    86
</verbatim>
colas@0
    87
expands to: %BR%
colas@0
    88
You %IF{"'%USERNAME%' ingroup 'TWikiAdminGroup'" then="are an admin" else="are a normal user"}% 
colas@0
    89
colas@0
    90
*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.
colas@0
    91
colas@0
    92
*Context identifiers* are used in TWiki to label various stages of the rendering process. They are especially useful for [[TWikiSkins][skin]] authors to find out where they are in the rendering process. The following context identifiers are available:
colas@0
    93
| *id* | *context* |
colas@0
    94
| absolute_urls | Set if absolute URLs are required |
colas@0
    95
| authenticated | a user is authenticated |
colas@0
    96
| body_text  | when the body text is being processed in a view (useful in plugin handlers) |
colas@0
    97
| can_login | current environment supports login |
colas@0
    98
| changes | in changes script (see TWikiScripts) |
colas@0
    99
| command_line | the running script was run from the command line, and not from CGI |
colas@0
   100
| diff | in rdiff script (see TWikiScripts) |
colas@0
   101
| edit | in edit script (see TWikiScripts) |
colas@0
   102
| footer_text | when the footer text is being processed in a view (useful in plugin handlers) |
colas@0
   103
| header_text | when the header text is being processed in a view (useful in plugin handlers) |
colas@0
   104
| i18n_enabled | when user interface <nop>I18N support is enabled (i.e., user can choose the language for UI) |
colas@0
   105
| inactive | if active links such as 'edit' and 'attach' should be disabled |
colas@0
   106
| mirror | if this is a mirror |
colas@0
   107
| new_topic | if the topic doesn't already exist |
colas@0
   108
| oops | in oops script (see TWikiScripts) |
colas@0
   109
| preview | in preview script (see TWikiScripts) |
colas@0
   110
| rss | if this is an RSS skin rendering |
colas@0
   111
| save | in save script (see TWikiScripts) |
colas@0
   112
| search | in search script (see TWikiScripts) |
colas@0
   113
| 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. |
colas@0
   114
| view | in view script (see TWikiScripts) |
colas@0
   115
| rest | in rest script (see TWikiScripts) |
colas@0
   116
| registration_supported | registration is supported by the current UserMapper |
colas@0
   117
| registration_enabled | set if ={Register}{EnableNewUserRegistration}= is on, and registrationis supported |
colas@0
   118
| passwords_modifyable | set if the password manager support changing the password / email |
colas@0
   119
colas@0
   120
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.
colas@0
   121
colas@0
   122
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.
colas@0
   123
colas@0
   124
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.