%META:TOPICINFO{author="ProjectContributor" date="1298378521" format="1.1" version="1"}%
%META:TOPICPARENT{name="DeveloperDocumentationCategory"}%
---+!! IF statements

The =%<nop>IF%= macro gives you the ability to test for macro values, URL parameters, access permissions, topic existence and more.

%TOC%

---++ Syntax
<verbatim class="tml">%IF{"CONDITION" then="THEN" else="ELSE"}%</verbatim>

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 [[QuerySearch][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 name or a wikiname. |
| =defined= | True if a [[PreferenceSettings][preference setting]] or url parameter of this name is defined. |
| =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='')= |
| =$= | 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 ="=. |

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.

---+++ Macros
---++++ Test if macro is defined
<verbatim class="tml">%IF{"defined MACRO" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
%IF{
   "defined WIKINAME"
   then="WIKINAME is defined"
   else="WIKINAME is not defined"
}%
</verbatim>

---++++ Compare macro definition
<verbatim class="tml">%IF{"$MACRO='VALUE'" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
You are %IF{
   "$WIKINAME='WikiGuest' and not defined 'OPEN_DAY'"
   then="not"
}% allowed to
%IF{
   "context view"
   then="view"
   else="edit"
}% this Foswiki today.
</verbatim>

---+++ URL parameters
---++++ Test if URL param is passed
<verbatim class="tml">%IF{"defined PARAM" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
%IF{
   "defined search"
   then="Search:%URLPARAM{"search"}%"
   else="No search passed in"
}%
</verbatim>

Note that macro =URLPARAM= is evaluated before the =IF= macro, therefore the expression does not need to be escaped.

---++++ Range test on URL parameter
<verbatim class="tml">%IF{"$PARAM < NUMBER" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
url param t is %IF{
   "0 < $t and $t < 1000"
   then="in"
   else="out of"
}% range.
</verbatim>

---++++ Text comparison of URL parameter
<verbatim class="tml">%IF{"$PARAM='VALUE'" then="THEN" else="ELSE"}%</verbatim>
or
<verbatim class="tml">%IF{"$'URLPARAM{PARAM}'='VALUE'" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
%IF{
   "$scope='text'"
   then="Plain text search"
}%
</verbatim>

or

<verbatim class="tml">
%IF{
   "$'URLPARAM{scope}'='text'"
   then="Plain text search"
}%
</verbatim>

#ParamTest
---++++ 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.

%TABLE{databg="#ffffff" headeralign="left" headerrows="2"}%
| *Test* \
| *IF query* \
| *no param* \
| *empty param* \
| *param=text* |
| \
| \
| [[%SCRIPTURL{"view"}%/%WEB%/%TOPIC%?paramtest=1;noparam=1#ParamTest][test: no param]] %IF{"$paramtest=1 and $noparam=1" then="<code>URLPARAM</code> value=%URLPARAM{"scope"}%."}% \
| [[%SCRIPTURL{"view"}%/%WEB%/%TOPIC%?paramtest=1;emptyparam=1;scope=#ParamTest][test: empty param]] %IF{"$paramtest=1 and $emptyparam=1" then="<code>URLPARAM</code> value=%URLPARAM{"scope"}%."}% \
| [[%SCRIPTURL{"view"}%/%WEB%/%TOPIC%?paramtest=1;paramvalue=1;scope=text#ParamTest][test: param=text]] %IF{"$paramtest=1 and $paramvalue=1" then="<code>URLPARAM</code> value=<code>%URLPARAM{"scope"}%</code>."}% |
| is defined \
| =defined scope= \
| %IF{"$paramtest=1 and $noparam=1" then='%IF{"defined scope" else='false'}%'}% \
| %IF{"$paramtest=1 and $emptyparam=1" then='%IF{"defined scope" then='true'}%'}% \
| %IF{"$paramtest=1 and $paramvalue=1" then='%IF{"defined scope" then='true'}%'}% |
| is empty \
| =$scope=''= \
| %IF{"$paramtest=1 and $noparam=1" then='%IF{"$scope=''" else='false %RED%NOTE: value is undefined, so not empty%ENDCOLOR%'}%'}% \
| %IF{"$paramtest=1 and $emptyparam=1" then='%IF{"$scope=''" then='true'}%'}% \
| %IF{"$paramtest=1 and $paramvalue=1" then='%IF{"$scope=''" else='false'}%'}% |
| is not empty \
| =$scope!=''= \
| %IF{"$paramtest=1 and $noparam=1" then='%IF{"$scope!=''" then='true %RED%NOTE: value is undefined, so also \\'not empty\\'%ENDCOLOR%'}%'}% \
| %IF{"$paramtest=1 and $emptyparam=1" then='%IF{"$scope!=''" else='false'}%'}% \
| %IF{"$paramtest=1 and $paramvalue=1" then='%IF{"$scope!=''" then='true'}%'}% |
| has value =text= \
| =$scope='text'"= \
| %IF{"$paramtest=1 and $noparam=1" then='%IF{"$scope='text'" else='false'}%'}% \
| %IF{"$paramtest=1 and $emptyparam=1" then='%IF{"$scope='text'" else='false'}%'}% \
| %IF{"$paramtest=1 and $paramvalue=1" then='%IF{"$scope='text'" then='true'}%'}% |

[[%SCRIPTURL{"view"}%/%WEB%/%TOPIC%#ParamTest][Reset test]]


---+++ INCLUDE parameters
<verbatim class="tml">%IF{"$PARAM" then="THEN" else="ELSE"}%</verbatim>

Parameters that are passed to the =INCLUDE= macro are evaluated the same as URL parameters. Combining those may lead to unexpected results!

Example:
<verbatim class="tml">
%INCLUDE{"%TOPIC%" section="people" name="Barry"}%
%STARTSECTION{"people"}%
%IF{"$name" then="%name%"}%
%ENDSECTION{"people"}%
</verbatim>

---++++ INCLUDE parameter test
<verbatim>
%INCLUDE{"%TOPIC%" section="includetest" name="Barry"}%
</verbatim>

%INCLUDE{"%TOPIC%" section="includetest" name="Barry"}%
<verbatim class='foswikiHidden'>
%STARTSECTION{"includetest"}%
%TABLE{databg="#ffffff" headeralign="left"}%
| *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"}%
</verbatim>


---+++ Configuration values
*Configuration items* are defined in [[%SCRIPTURLPATH{"configure"}%][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  %FORMAT{"%QUERY{"{AccessibleCFG}"}%" type="string" format="=$item=" separator=", "}%.

---++++ Testing if item is set
<verbatim class="tml">%IF{"{CONFIGURATION}" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
User details are %IF{
   "{AntiSpam}{HideUserDetails}"
   then="hidden"
   else="shown"
}%
</verbatim>

---++++ Testing the value of an item
<verbatim class="tml">%IF{"{CONFIGURATION}='VALUE'" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
Validation method is %IF{
   "{Validation}{Method}='strikeone'"
   then="'strikeone'"
   else="no 'strikeone'"
}%
</verbatim>

---+++ Plugin states
<verbatim class="tml">%IF{"context PLUGINEnabled" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
TablePlugin is %IF{
   "context TablePluginEnabled"
   then="enabled"
   else="disabled"
}%.
</verbatim>

---+++ Access permissions
<verbatim class="tml">%IF{"'TOPIC' allows 'ACCESS'" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
You %IF{"'%TOPIC%' allows 'change'" then="can" else="cannot"}% change this topic.
You %IF{"'%SANDBOXWEB%.TestTopic' allows 'change'" then="can" else="cannot"}% change %SANDBOXWEB%.TestTopic.
You %IF{"'%SANDBOXWEB%' allows 'change'" then="can" else="cannot"}% change %SANDBOXWEB% web
</verbatim>

---+++ Topic existence
<verbatim class="tml">%IF{"istopic 'TOPIC'" then="THEN" else="ELSE"}%</verbatim>

Example:
<verbatim class="tml">
Topic %SANDBOXWEB%.TestTopic %IF{"istopic '%SANDBOXWEB%.TestTopic'" then="exists" else="does not exist"}%.
Web %SANDBOXWEB% %IF{"isweb '%SANDBOXWEB%'" then="exists" else="does not exist"}%.
</verbatim>

---+++ Group membership
To test if SOMEONE is in the GROUP:
<verbatim class="tml">%IF{"'SOMEONE' ingroup 'GROUP'" then="THEN" else="ELSE"}%</verbatim>

Or to test the currently logged in user for !AdminGroup membership:
<verbatim class="tml">
You are %IF{
   "$USERNAME ingroup 'AdminGroup'"
   then="an admin"
   else="a normal user"
}%
</verbatim>

---+++ Context identifiers
<verbatim class="tml">%IF{"context CONTEXT" then="THEN" else="ELSE"}%</verbatim>

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:
| *id* | *context* |
| =absolute_urls= | Set if absolute URLs are required |
| =attach= | in attach script (see CommandAndCGIScripts) |
| =authenticated= | a user is authenticated |
| =body_text=  | when the body text is being processed in a view (useful in plugin handlers) |
| =can_login= | current environment supports login |
| =changes= | in changes script (see CommandAndCGIScripts) |
| =command_line= | the running script was run from the command line, and not from CGI |
| =diff= | in rdiff script (see CommandAndCGIScripts) |
| =edit= | in edit script (see CommandAndCGIScripts) |
| =footer_text= | when the footer text is being processed in a view (useful in plugin handlers) |
| =header_text= | when the header text is being processed in a view (useful in plugin handlers) |
| =i18n_enabled= | when user interface <nop>I18N support is enabled (i.e., user can choose the language for UI) |
| =inactive= | if active links such as 'edit' and 'attach' should be disabled |
| =login= and =logon= | in login / logon script (see CommandAndCGIScripts) |
| =manage= | in manage script (see CommandAndCGIScripts) |
| =mirror= | if this is a mirror |
| =new_topic= | if the topic doesn't already exist |
| =oops= | in oops script (see CommandAndCGIScripts) |
| =preview= | in preview script (see CommandAndCGIScripts) |
| =register= | in register script (see CommandAndCGIScripts) |
| =rename= | in rename script (see CommandAndCGIScripts) |
| =resetpasswd= | in resetpasswd script (see CommandAndCGIScripts) |
| =rss= | if this is an RSS skin rendering |
| =save= | in save script (see CommandAndCGIScripts) |
| =search= | in search script (see CommandAndCGIScripts) |
| =static= | in a script that generates static content, such as PDF generation. Extensions should avoid rendering edit markup in this context. |
| =statistics= | in statistics script (see CommandAndCGIScripts) |
| =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. |
| =upload= | in upload script (see CommandAndCGIScripts) |
| =view= | in view script (see CommandAndCGIScripts) |
| =viewfile= | in viewfile script (see CommandAndCGIScripts) |
| =rest= | in rest script (see CommandAndCGIScripts) |
| =registration_supported= | registration is supported by the current user mapper |
| =registration_enabled= | set if ={Register}{EnableNewUserRegistration}= is on, and registration is supported |
| =passwords_modifyable= | set if the password manager supports changing the password / email |

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.


---++ Query syntax
<verbatim class="tml">%IF{"QUERY" then="THEN" else="ELSE"}%</verbatim>

---+++ Examples
Display the value of a form field if the topic has form field "Summary":
<verbatim class="tml">
%FORMFIELD{
   "%IF{
      "'%TOPIC%'/Summary"
      then="Summary"
   }%"
}%
</verbatim>
Note that while a [[QuerySearch][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:

<blockquote>%H% The [[VarIF][IF]] macro is expanded before [[VarFORMFIELD][FORMFIELD]]. [[FormattedSearch#InsideOutLeftToRight][Read more]]</blockquote>

Test if the topic has attachments:
<verbatim class="tml">
%IF{
   "'%TOPIC%'/attachments"
   then="has attachments"
}%
</verbatim>

Test if the topic has an attachment with 'receipt' or 'Receipt' in the name:
<verbatim class="tml">
%IF{
   "'%TOPIC%'/attachments[lc(name)~'*receipt*']"
   then="has 'receipt' attachment"
}%
</verbatim>

Test if a topic text contains a certain word:
<verbatim class="tml">
%IF{
   "'%SYSTEMWEB%.WebHome'/lc(text)~'*welcome*'"
   then="contains 'welcome'"
}%
</verbatim>
<!-- %JQREQUIRE{"chili"}% -->
