In the example above, if CONDITION evaluates to TRUE, then THEN will be included in the topic; otherwise ELSE will be included.
Note that because of the way Foswiki evaluates, 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.
The basic syntax of a condition is the same as the syntax used for queries, with the addition of the following special operators:
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 username or a WikiName.
True if a preference setting, URL parameter or session variable of this name has an empty value. It is equivalent to the expression (not defined(x) || $x='')
$
expands a URL parameter or 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 ".
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.
Macros
Test if macro is defined
%IF{"defined MACRO" then="THEN" else="ELSE"}%
Example:
%IF{
"defined WIKINAME"
then="WIKINAME is defined"
else="WIKINAME is not defined"
}%
Compare macro definition
%IF{"$MACRO='VALUE'" then="THEN" else="ELSE"}%
Example:
You are %IF{
"$WIKINAME='WikiGuest' and not defined 'OPEN_DAY'"
then="not"
}% allowed to
%IF{
"context view"
then="view"
else="edit"
}% this Foswiki today.
%IF{
"$'URLPARAM{scope}'='text'"
then="Plain text search"
}%
URL parameter test
Be careful when you test for an empty value: if the URL parameter is not defined (not passed in the URL), the outcome might not be what you expected. Use the parameter test to check your assumptions.
false NOTE: value is undefined, so not 'not empty'
has value Barry
$name='Barry'
true
$yo='Barry'
false
%STARTSECTION{"includetest"}%
| *Test* \
| *IF query param 'name'* \
| *Result param 'name'* \
| *IF query param 'yo'* \
| *Result param 'yo'* |
| is defined \
| =defined name= \
| %IF{"defined name" then='true'}% \
| =defined yo= \
| %IF{"defined yo" else='false'}% |
| is empty \
| =$name=''= \
| %IF{"$name=''" else='false'}% \
| =$yo=''= \
| %IF{"$yo=''" else="false %RED%NOTE: value is undefined, so not empty%ENDCOLOR%"}% |
| is not empty \
| =$name!=''= \
| %IF{"$name!=''" then='true'}% \
| =$yo!=''= \
| %IF{"$yo=''" else="false %RED%NOTE: value is undefined, so not \'not empty\'%ENDCOLOR%"}% |
| has value =Barry= \
| =$name='Barry'= \
| %IF{"$name='Barry'" then='true'}% \
| =$yo='Barry'= \
| %IF{"$yo='Barry'" else='false'}% |
%ENDSECTION{"includetest"}%
Configuration values
Configuration items are defined in configure. You cannot see the value of a configuration item, you can only test if the item is set, or test the value against a string.
Items must be one of {AccessControlACL}{EnableDeprecatedEmptyDeny}, {AccessibleCFG}, {AdminUserLogin}, {AdminUserWikiName}, {AntiSpam}{EmailPadding}, {AntiSpam}{EntityEncode}, {AntiSpam}{HideUserDetails}, {AntiSpam}{RobotsAreWelcome}, {AttachmentNameFilter}, {AuthRealm}, {AuthScripts}, {Cache}{Enabled}, {DefaultDateFormat}, {DefaultUrlHost}, {DenyDotDotInclude}, {DisplayTimeValues}, {EnableEmail}, {EnableHierarchicalWebs}, {FormTypes}, {HomeTopicName}, {LeaseLength}, {LeaseLengthLessForceful}, {LinkProtocolPattern}, {LocalSitePreferences}, {LoginNameFilterIn}, {MaxRevisionsInADiff}, {MinPasswordLength}, {NameFilter}, {NotifyTopicName}, {NumberOfRevisions}, {PluginsOrder}, {Plugins}{WebSearchPath}, {PluralToSingular}, {Register}{AllowLoginName}, {Register}{Approvers}, {Register}{DisablePasswordConfirmation}, {Register}{EnableNewUserRegistration}, {Register}{NeedApproval}, {Register}{NeedVerification}, {Register}{RegistrationAgentWikiName}, {ReplaceIfEditedAgainWithin}, {SandboxWebName}, {ScriptSuffix}, {ScriptUrlPath}, {Site}{Locale}, {SitePrefsTopicName}, {Stats}{TopContrib}, {Stats}{TopicName}, {Stats}{TopViews}, {SuperAdminGroup}, {SystemWebName}, {TemplateLogin}{AllowLoginUsingEmailAddress}, {TemplatePath}, {TrashWebName}, {UploadFilter}, {UseLocale}, {UserInterfaceInternationalisation}, {UsersTopicName}, {UsersWebName}, {Validation}{Method}, {WebMasterEmail}, {WebMasterName}, {WebPrefsTopicName}.
Testing if item is set
%IF{"{CONFIGURATION}" then="THEN" else="ELSE"}%
Example:
User details are %IF{
"{AntiSpam}{HideUserDetails}"
then="hidden"
else="shown"
}%
Or to test the currently logged in user for AdminGroup membership:
You are %IF{
"$USERNAME ingroup 'AdminGroup'"
then="an admin"
else="a normal user"
}%
Context identifiers
%IF{"context CONTEXT" then="THEN" else="ELSE"}%
Context identifiers are used in Foswiki 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:
if user is currently running with internal admin authority
SUPPORTS_PARA_INDENT
render supports the paragraph indent syntax
SUPPORTS_PREF_SET_URLS
Preferences can be set in the URL
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.
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.
JQueryPlugin sets a context for each enabled JQuery plugin, for example: LiveQueryEnabled, UI::DialogEnabled. Other extensions may set additional context identifiers.
Query syntax
%IF{"QUERY" then="THEN" else="ELSE"}%
Examples
Display the value of a form field if the topic has form field "Summary":
Note that while a query search 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 want. In these cases you can use the / operator to indicate the name of the topic: