You are here:
Foswiki
>
System Web
>
IfDefinedPlugin
Edit
Attach
Tags:
create new tag
,
view all tags
---+!! <nop>%TOPIC% %TOC% ---++ Description <table style="float:right"> <tr> <td><img src="%ATTACHURLPATH%/wikiringlogo40x40.png"></td> <td><a href="http://wikiring.com" title="Make your Wiki ring!" style="text-decoration:none"> Powered by <br /> <nop>WikiRing Consultants </a> </td> </tr> </table> This plugin adds a couple of tags that allow to render content conditionally. There are two different ways to do that: either by using the inline =%<nop>IFDEFINED{"string" then="..." else="..."}%= tag that returns the =then= or =else= argument depending on the =string= being defined, that is non-empty. There are no other ways to decide the condition but rendering the =test= string being a <nop>TopicMarkup expression, for example by embedding a =%<nop>CALC{}%= expression. This simplifies this plugin alot and allows a rather flexible way to generate the condition. The other way is to use blocks of content surrounded by expressions like this: <verbatim> %IFDEFINEDTHEN{"test"}% ... %ELSIFDEFINED{"test"}% ... %ELSEDEFINED% ... %FIDEFINED% </verbatim> This way the conditioned content is not limited to fit into a the argument position of a =%<nop>IFDEFINED{...}%= expression. Blocks can be nested and are evaluated starting from the outside, contrary to standard <nop>TopicMarkup being evaluated left-to-right-inside-out. ---++ Related work An %IF conditional is part of Foswiki as well. See Foswiki:System.IfStatements. The main differences between IF and IFDEFINED are 1 IF comes with a fullfledged boolean expression parser whereas IFDEFINED only checks if a test expression expands to a non-empty string. 1 This plugin comes with separate conditionals IFEXISTS and IFACCESS to implement some of the most commonly used tests in <nop>WikiApplications 1 IFDEFINED's sister IFDEFINEDTHEN also provides a section-oriented conditional 1 IFDEFINED comes with a "glue" feature to remov whitesapce before and after the conditional statement (can be switched off optionally) ---++ Syntax Rules ---+++ IFDEFINED *Syntax*: <verbatim> %IFDEFINED{"test" [action="view,edit,preview,..."]? [as="..."] then="..." else="..." glue="on,off"}% </verbatim> returns the "then or "else" content depending on the "test" expression expanding to the emtpy string or matching the string given in "as". Optionally the current topic action is checked to match the one given in "action". Normally, all surrounding whitespaces are removed, that is the resulting content is glued together in a way. You may switch that off by setting "glue" to "off" thus preventing all whitespace as it is. The "then" and "else" strings may contain the following pseudo-variables: * =$n=: substituted with a newline character (\n) * =$percnt=: substituted with a percent character (%) * =$dollar=: substituted with a dollar character ($) * =$test= or =$variable=: substituted with the "test" string * =$value=: substituted with the "as" string ---+++ IFDEFINEDTHEN, ELSIFDEFINED, ELSEDEFINED, FIDEFINED *Syntax*: <verbatim> %IFDEFINEDTHEN{"test" [action="view,edit,preview,..."]?}% <then content> [%ELSIFDEFINED{...}% <elsif content>]* [%ELSEDEFINED% <else content>]? %FIDEFINED% </verbatim> This is the multi-line version of [[#IFSDEFINED][IFSDEFINED]]. IFDEFINEDTHEN expressions can be nested safely. Within a single IFDEFINEDTHEN the ELSIFDEFINED parts can be repeated arbitrarily. Before rendering the =<then|elsif|else condition>= any =$nop= string will be removed first. Thereby nested <nop>TopicMarkup expressions can be prevented from being expanded in advance. ---+++ IFACCESS *Syntax*: <verbatim> %IFACCESS% %IFACCESS{"<webtopic>" [type="view|change"] [user="<WikiName>"] [then="<then-clause>"] [else="<else-clause>"] [glue="on|off"]}% </verbatim> This checks the access permissions a user has on a given topic and then renders the =<then-clause>= if access is granted and =lt;else-clause>= otherwise. By default, the current user's view access will be checked on the current topic (=%<nop>IFACCESS%=). The other parameters are: * =<webtopic>=: the name of the topic to be checked, defaults to current * =user="<WikiName>"=: the name of the user to be checked, defaults to the current user * =type="view|change"=: the type of access to be checked, either view access or change/edit access, defaults to "view" * =then="<then-clause>"=: the string to be returned on granted access, defaults to "1" * =else="<else-clause>"=: the string to be returned if no access is granted, defaults to "0" * =glue="on|off"=: apply glue around the IFACCESS tag or not, defaults to "on" ---+++ IFEXISTS *Syntax*: <verbatim> %IFEXISTS{"<webtopic>" [then="<then-clause>"] [else="<else-clause>"] [glue="on|off"]}% </verbatim> This tag checks if =webtopic= exists and expands =then-clause= or =else-clause= as needed. A similar functionality is available using =%<nop>CALC{"$IF($EXIST(<webtopic>,<then-clause>),<else-clause>)"}%= of the SpreadSheetPlugin. This is still preferable if you need more computation based on CALC in the then/else branches. The IFEXISTS version however is more lightweigted, comes with a much simpler syntax and allows more complex <nop>TopicMarkup in the then/else branches. ---++ Demo * Set FOO = foo * FOO is %IFDEFINED{"%FOO%" then=" " else="not" glue="off"}% defined. * FOO is %IFDEFINED{"%FOO%" as="bar" then=" " else="not" glue="off"}% defined as "bar". * BAR is %IFDEFINED{"%BAR%" then=" " else="not" glue="off"}% defined. * %WIKIUSERNAME% has %IFACCESS{type="change" then=" " else=" no "}% write access to %TOPIC% * %MAINWEB%.WikiGuest has %IFACCESS{user="WikiGuest" type="change" then=" " else=" no "}% write access to %TOPIC% ---++ Plugin Installation Instructions You do not need to install anything in the browser to use this extension. The following instructions are for the administrator who installs the extension on the server. Open configure, and open the "Extensions" section. Use "Find More Extensions" to get a list of available extensions. Select "Install". If you have any problems, or if the extension isn't available in =configure=, then you can still install manually from the command-line. See http://foswiki.org/Support/ManuallyInstallingExtensions for more help. ---++ Plugin Info <!-- provided for compatibility only * Set SHORTDESCRIPTION = Render content conditionally --> | Plugin Author: | Michael Daum | | Copyright ©: | 2006-2009, Michael Daum http://michaeldaumconsulting.com | | License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) | | Release: | v2.00 | | Version: | 4422 (2009-07-03) | | Change History: | <!-- specify latest version first --> | | 23 Apr 2009: | converted to foswiki plugin | | 07 Jan 2009: | certified for foswiki/compat | | 03 Jul 2008: | more rookie perl errors | | 05 Oct 2007: | fixed a rookie perl error | | 20 Jun 2007: | added IFEXISTS ;\ make use of TWiki-4 APIs for speed improvements | | 18 Dec 2006: | removed support for cairo and beijing; added IFACCESS tag: checks access of some user on a topic | | 18 Sep 2006: | allow multinline values to be checked | | 31 Aug 2006: | added NO_PREFS_IN_TOPIC | | 15 Aug 2006: | added =$test= and =$value= | | 07 Aug 2006: | be smarter to detect the cgi action wrt to different apache setups | | 10 Mar 2006: | added =as= parameter; \ added escape parameters and recursive variable expansion to the condition argument \ which helps to circumvent problems due to different plugin orders | | 21 Feb 2006: | Initial version | | CPAN Dependencies: | none | | Other Dependencies: | none | | Perl Version: | 5.8 | | Plugin Home: | Foswiki:Extensions/%TOPIC% | | Feedback: | Foswiki:Support/%TOPIC% |
Edit
|
Attach
|
P
rint version
|
H
istory
:
r4
<
r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
Edit WikiText
|
More topic actions...
Topic revision: r1 - 12 Nov 2013,
UnknownUser
System
Log In
or
Register
Toolbox
Users
Groups
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
User Reference
BeginnersStartHere
TextFormattingRules
Macros
FormattedSearch
QuerySearch
DocumentGraphics
SkinBrowser
InstalledPlugins
Admin Maintenance
Reference Manual
AdminToolsCategory
InterWikis
ManagingWebs
SiteTools
DefaultPreferences
WebPreferences
Categories
Admin Documentation
Admin Tools
Developer Doc
User Documentation
User Tools
Webs
Applications
ClassificationApp
Extensions
Main
System
Copyright © by the contributing authors. All material on this site is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki?
Send feedback