Tags:
create new tag
, view all tags
ALERT! NOTE: This is a DistributionDocument. Please help maintain high quality documentation: This is a wiki, please fix the documentation if you find errors or incomplete content. Put questions and suggestions concerning the documentation of this topic in the comments section below. Use the Support web for problems you are having using TWiki.

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 included in the topic; otherwise ELSE will be included.

What can be included in the THEN and ELSE parameters is obviously limited by standard TWiki syntax for parameters. The best way to use this feature is to define two TWikiVariables, one for the THEN case and the other for the ELSE case. For example:

<!--
   * Set LI = Fred is here!
   * Set LO = Fred is there!
-->
%IF{"context authenticated and defined WIKINAME and $ WIKINAME='FredBloggs'" then="%LI%" else="%LO%"}%

The basic syntax of a condition is as follows:

expr ::= '(' expr ')' ;
expr ::= andexpr | andexpr 'or' expr ;
andexpr ::= notexpr | notexpr 'and' andexpr ;
notexpr ::= basexpr | 'not' baseexpr ;
basexpr ::= atom | uop atom | atom bop basexpr ;
uop ::= 'context' | 'defined' | '$' ;
bop ::= '=' | '!=' | '>' | '<' | '>=' | '<=' ;
atom ::= context identifier, TWiki variable name, single-quoted string
Operators
and True if both sides are true
or True if one or other side is true
not negate the following expression
=, != String comparison
<, >, <=, >= Number comparison (there is no explicit numeric =)
context True if the current context is set (see below)
defined True if a preference variable or url parameter of this name is defined.
$ expands a URL parameter or TWikiVariable. 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 quoted string may not contain quotes (' or ").

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.

3. URL parameter

%IF{ "defined search" then="Search: $percntURLPARAM{search}$percnt" else="No search passed in"}%

4. Range test on URL parameter

url param t is %IF{ "0 < $ t and $ t < 1000" then="in" else="out of"}% range.

5. Text comparison of URL parameter

%IF{ "$'URLPARAM{scope}'='text'" then="Plain text search" }% 

6. Configuration item set or not

%IF{ "{AntiSpam}{HideUserDetails}" then="User details are hidden" }%

7. Plugin enabled test

TablePlugin is %IF{ "context TablePluginEnabled" then="enabled" else="disabled" }%.
expands to:
TablePlugin is enabled.

Context identifiers 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:

id context
absolute_urls Set if absolute URLs are required
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 TWikiScripts)
command_line the running script was run from the command line, and not from CGI
diff in rdiff script (see TWikiScripts)
edit in edit script (see TWikiScripts)
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 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
mirror if this is a mirror
new_topic if the topic doesn't already exist
oops in oops script (see TWikiScripts)
preview in preview script (see TWikiScripts)
rss if this is an RSS skin rendering
save in save script (see TWikiScripts)
search in search script (see TWikiScripts)
view in view script (see TWikiScripts)
rest in rest script (see TWikiScripts)

In addition there is an identifier for each enabled plugin; for example, if GallousBreeksPlugin is installed and enabled, then the context ID GallousBreeksPluginEnabled will be set.

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.



Comments & Questions about this Distribution Document Topic

Greetings! I've been pulling my hair out for 6 hours trying to solve an issue, and finally got it. It has to do with conditional setting of variables.

Here's my code, the goal being to define a value for LFS based on either a URL parameter or a session variable:

   * Set PARAM_OFFERED = %IF{ "$'URLPARAM{locationfeatureset}'=''" then="false" else="true" }%
   * Set PARAM_VALUE= 
   * Set SESSION_OFFERED = %IF{ "$'SESSION_VARIABLE{locationfeatureset}'=''" then="false" else="true" }%
   * Set SESSION_VALUE = %SESSION_VARIABLE{"locationfeatureset"}%

PARAM_OFFERED: %PARAM_OFFERED% <br>
PARAM_VALUE: %PARAM_VALUE% <br>
SESSION_OFFERED: %SESSION_OFFERED% <br>
SESSION_VALUE: %SESSION_VALUE%

   * Set LFS_DEFAULT = %IF{ "%SESSION_OFFERED%=true" then="%SESSION_VALUE%" else="on" }%
   * Set LFS = %IF{ "%PARAM_OFFERED%=true" then="%PARAM_VALUE%" else="%LFS_DEFAULT%" }%

The error was I was using LSF in the place of LFS_DEFAULT. By having setting a variable and having the return be one of the variables, it causes messy loops:

   * Set VAR = %IF{ "1=1" then="%VAR% else="false" }%

Would you make a comment in the IfStatements topic that you shouldn't do such setting? thanks! Ken

-- KenBonninJr - 08 Nov 2006

The IF can be a bit complex. In fact, I do not understand what you are trying to document. Could you give it a try? Simply edit the main document part on top.

-- PeterThoeny - 12 Nov 2006

It seems I can't use an %IF{} inside of WebLeftBar. When I view WebLeftBar directly, my %IF{} statement works. But when viewing the Web itself, I see the first part of the %IF{} statement itself on the LeftBar.

-- MarkKeisler - 25 Jan 2007

This looks like a Support question, possibly a %TOPIC% vs. %BASETOPIC% issue.

-- PeterThoeny - 27 Jan 2007

Can I use this to detect if some plugin is enabled/installed?

-- MikkoLaakso - 30 Jun 2007

Found it, added examples.

-- MikkoLaakso - 30 Jun 2007

I could not make the example 3 work in my TWiki 4.1.2:

%IF{ "defined search" then="Search: $percntURLPARAM{search}$percnt" else="No search passed in"}%

Test: give the parameter "search" in url (url + ?search=something): No search passed in

Seems like $percnt does not expand? This means I cannot use conditionals to give additional functionality, if a plugin is on.

-- MikkoLaakso - 15 Jul 2007

I'm going mad trying to get something to work. See Support.QueryByAuthor for details

The following condition fails. The results show that the condition should NOT fail; most of the results were created, or modified, by me. Nevertheless, the condition fails consistently.

What am I doing wrong?

%SEARCH{"." scope="topic" regex="on" nonoise="on" limit="100" format="$percntIF{\"$ wikiname=VickiBrown or $createwikiname=VickiBrown\" then=\"|YES| [[$topic]] | $createwikiusername | $wikiusername |\" else=\"|NO| [[$topic]] |  $createwikiname |  $wikiname |\"}$percnt"     }% 

-- VickiBrown - 22 Jul 2007

Apparently what I was doing wrong was the space after the $... even though all of the examples for %IF insert a space after the $

-- VickiBrown - 22 Jul 2007

Even though it says plugin handlers are not called and only URL parameters, standard TWiki variables or preference variables are supported, I was still able to get an IF statement to work that evaluated a Workflow plugin variable %WORKFLOWSTATE%. Is this a fluke or has the IF variable been modified to allow for plugin variables?

-- GarySprague - 27 Jun 2008

 
Topic revision: r14 - 27 Jun 2008 - 17:56:04 - GarySprague
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback