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:
%IF{"defined WIKINAME" then="WIKINAME is defined" else="WIKINAME is not defined"}%

You are %IF{ "$ WIKINAME='TWikiGuest' and not defined OPEN_DAY" then="not" }% allowed to
%IF{ "context view" then="view" else="edit"}% this TWiki today.

%IF{ "defined search" then="Search: %URLPARAM{search}%" else="No search passed in"}%

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

%IF{ "$'URLPARAM{scope}'='text'" then="Plain text search" }% 
Given
  • Set OPEN_DAY = yes
this expands to:

You are not allowed to view this TWiki today.

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 contextSorted ascending
authenticated a user is authenticated
can_login current environment supports login
inactive if active links such as 'edit' and 'attach' should be disabled
new_topic if the topic doesn't already exist
mirror if this is a mirror
rss if this is an RSS skin rendering
changes in changes script (see TWikiScripts)
edit in edit script (see TWikiScripts)
oops in oops script (see TWikiScripts)
preview in preview script (see TWikiScripts)
diff in rdiff script (see TWikiScripts)
rest in rest script (see TWikiScripts)
save in save script (see TWikiScripts)
search in search script (see TWikiScripts)
view in view script (see TWikiScripts)
absolute_urls Set if absolute URLs are required
command_line the running script was run from the command line, and not from CGI
body_text when the body text is being processed in a view (useful in plugin handlers)
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)

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.

Edit | Attach | Watch | Print version | History: r12 | r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2006-02-01 - TWikiContributor
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Note: Please contribute updates to this topic on TWiki.org at TWiki:TWiki.IfStatements.