The %IF% construct gives TWiki the power to include content in topics based on the value of simple expressions.
%IF{"CONDITION" then="THEN" else="ELSE"}%
In the example above, if CONDITION evaluates to TRUE, then THEN will be shown in the topic; otherwise ELSE will be shown.
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 such as $percnt for % can be used in the THEN and ELSE parameters when you need to delay evaluation of (for example) a TWiki variable.
The basic syntax of a condition is the same as the syntax used for queries, with operators=, !=, ~, <, >, <=, >=, NOT, AND, OR, (), and functionslc(), uc(), d2n().
In addition, the following special operators are supported:
context
True if the current context is set (see below)
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'.
istopic
istopic 'X' is true if topic 'X' exists
isweb
isweb 'X' is true if web 'X' exists
ingroup
'X' ingroup 'Y' is true if user 'X' is in group 'Y'. 'X' can be a login name or a wikiname.
defined
True if a preference variable or url parameter of this name is defined.
isempty
True if a preference variable, url parameter or session variable of this name has an empty value. It is equivalent to the expression (defined(x) || $x='')
$
expands a URL parameter or 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 ").
{X}
expands to the value of the configuration variable {X} - for example, {ScriptUrlPath}
You can test if a configuration item is set or not, using conditions such as "{AntiSpam}{HideUserDetails}". It is not possible see the value of a configuration. Configuration items are defined in configure.
You can test if a context identifier is set or not, using conditions such as "context authenticated". Context identifier are used in TWiki to label various stages of the rendering process. They are especially useful for skin authors to find out where they are in the rendering process. The following context identifiers are available:
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.
registration is supported by the current UserMapper
registration_enabled
set if {Register}{EnableNewUserRegistration} is on, and registrationis supported
passwords_modifyable
set if the password manager support changing the password / email
In addition there is a context identifier for each enabled plugin; for example, if GallousTwistyPlugin is installed and enabled, then the context ID GallousTwistyPluginEnabled will be set. Other extensions may set additional context identifiers.
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.
Note also that while the 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.
Examples
1. TWiki variable defined or not
%IF{"defined 'WIKINAME'" then="WIKINAME is defined" else="WIKINAME is not defined"}%
2. Compare TWiki variable
You are %IF{ "$ WIKINAME='TWikiGuest' and not defined 'OPEN_DAY'" then="not" }% allowed to
%IF{ "context view" then="view" else="edit"}% this TWiki today.
The ExecutiveReport topic is only included if the user is in the ExecutiveGroup.
12. Hide section of text conditionally using CSS display
<div style="display: %IF{"'%USERNAME%' ingroup 'TWikiAdminGroup'" then="block" else="none"}%">
* Conditional text enclosed in div tags here...
* ...can be as long as needed
</div>