component2Add my vote for this tag development1Add my vote for this tag twiki_application2Add my vote for this tag create new tag
, view all tags


Render content conditionally


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 %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 TWikiML expression, for example by embedding a %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:


This way the conditioned content is not limited to fit into a the argument position of a %IFDEFINED{...}% expression. Blocks can be nested and are evaluated starting from the outside, contrary to standard TWikiML being evaluated left-to-right-inside-out.

Related work

Similar extensions have been done before but are either only available as an integral part of TWiki-4.0 (see TWiki:TWiki/IfStatements) or unmaintained (see TWiki:Plugins/ConditionalPlugin). Especially these two alternatives are rather similar as they defined the same tag (%IF). The main differences between those and this plugin here is that they have a more elaborated condition parser that allows to evaluate more complex boolean expressions. Some of the expressions in %IF of TWiki-4.0 are specific to that platform and make only sense there (context). The other difference is that the IFDEFINED tag family allows nested blocks of conditional content and %IF is an inline tag only.

The advantage of this plugin is that you get a simpler mechanism for checking the condition which is however more flexible as the string can be generated arbitrarily using TWikiML. Furthermore, and possibly more important, this plugin is backwards compatible and may be used on a TWiki/Beijing, TWiki/Cairo and TWiki/Dakar engine.

This plugin has been externalized from the TWiki:Plugins/NatSkinPlugin.

Syntax Rules




returns the "then or "else" content depending on the "test" expression expanding to the empty 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{"test" [action="view,edit,preview,..."]?}% 
  <then content> 
  <elsif content>]* 
  <else content>]?

This is the multi-line version of 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 TWikiML expressions can be prevented from being expanded in advance.





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 (%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"




This tag checks if webtopic exists and expands then-clause or else-clause as needed. A similar functionality is available using %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 TWikiMarkup in the then/else branches.


  • 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.
  • TWikiGuest has %IFACCESS{type="change" then=" " else=" no "}% write access to IfDefinedPlugin
  • TWikiGuest has %IFACCESS{user="TWikiGuest" type="change" then=" " else=" no "}% write access to IfDefinedPlugin

Plugin Installation Instructions

  • Download the ZIP file (see below)
  • Unzip it in your twiki installation directory. Content:
    File: Description:
    data/TWiki/IfDefinedPlugin.txt Plugin topic
    lib/TWiki/Plugins/IfDefinedPlugin.pm Plugin Perl module
  • Visit configure in your TWiki installation, and enable the plugin in the {Plugins} section.

Plugin Info

Plugin Author: TWiki:Main.MichaelDaum
Copyright: © 2006-2008, Michael Daum http://michaeldaumconsulting.com
© 2006-2010 TWiki:TWiki/TWikiContributor
License: GPL (GNU General Public License)
Plugin Version: v1.1
Change History:  
2010-04-29: TWikibug:Item6433 - doc improvements
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
TWiki:Plugins/Benchmark: GoodStyle nn%, FormattedSearch nn%, IfDefinedPlugin nn%
Plugin Home: TWiki:Plugins/IfDefinedPlugin
Feedback: TWiki:Plugins/IfDefinedPluginDev

Related Topics: VarIF, IfStatements, SpreadSheetPlugin, TWikiPlugins, DeveloperDocumentationCategory, AdminDocumentationCategory, TWikiPreferences

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatmd5 IfDefinedPlugin.md5 r15 r14 r13 r12 r11 manage 0.2 K 2010-04-29 - 23:01 PeterThoeny  
Compressed Zip archivetgz IfDefinedPlugin.tgz r15 r14 r13 r12 r11 manage 7.1 K 2010-04-29 - 23:01 PeterThoeny  
Compressed Zip archivezip IfDefinedPlugin.zip r16 r15 r14 r13 r12 manage 8.7 K 2010-04-29 - 23:01 PeterThoeny  
Unknown file formatEXT IfDefinedPlugin_installer r9 r8 r7 r6 r5 manage 3.4 K 2010-04-29 - 23:01 PeterThoeny  
Edit | Attach | Watch | Print version | History: r23 < r22 < r21 < r20 < r19 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r23 - 2013-10-16 - PeterThoeny
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2016 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.