|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IF StatementsThe%IF% construct gives Foswiki the power to include content in topics based on the value of simple expressions.
On this page:
Syntax%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.
Note that because of the way Foswiki 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 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:
Examples1. Macro defined or not%IF{"defined 'WIKINAME'" then="WIKINAME is defined" else="WIKINAME is not defined"}%2. Compare macro definition You are %IF{ "$ WIKINAME='WikiGuest' and not defined 'OPEN_DAY'" then="not" }% allowed to %IF{ "context view" then="view" else="edit"}% this Foswiki 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. 8. Check access permissions You %IF{"'%TOPIC%' allows 'change'" then="can" else="cannot"}% change this topic. You %IF{"'Sandbox.TestTopic' allows 'change'" then="can" else="cannot"}% change Sandbox.TestTopic. You %IF{"'Sandbox' allows 'change'" then="can" else="cannot"}% change Sandbox webexpands to: You cannot change this topic. You can change TestTopic. You can change Sandbox web 9. Check topic existance Topic Sandbox.TestTopic %IF{"istopic 'Sandbox.TestTopic'" then="exists" else="does not exist"}% Web Sandbox.TestTopic %IF{"isweb 'Sandbox'" then="exists" else="does not exist"}%expands to: Topic TestTopic exists Web TestTopic exists 10. Group membership You %IF{"'%USERNAME%' ingroup 'AdminGroup'" then="are an admin" else="are a normal user"}%expands to: You are a normal user Configuration items are defined in configure. You cannot see the value of a configuration item, you can only see if the item is set or not. Context identifiersContext 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:
GallousBreeksPlugin is installed and enabled, then the context ID GallousBreeksPluginEnabled 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.
Query syntaxNote 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.ExamplesDisplay the value of a form field if the topic has form field "Summary":%FORMFIELD{"%IF{"'%TOPIC%'/Summary" then="Summary"}%"}%Test if the topic has attachments: %IF{"'%TOPIC%'/attachments" then="has attachments"}%Test if the topic has an attachment with 'receipt' or 'Receipt' in the name: %IF{"'%TOPIC%'/attachments[lc(name)~'*receipt*']" then="has 'receipt' attachment"}%Test if a topic text contains a certain word: %IF{"'%SYSTEMWEB%.WebHome'/lc(text)~'*welcome*'" then="contains 'welcome'"}% |