Syntax

The generic form of the (:if:) directive is

    (:if cond param:) body (:if:)

where “cond” names a condition to be tested from the $Conditions array, and “param” is a parameter or other argument held in $condparm in the test. For example, the following (in config.php) adds (:if group GroupName:) as a markup:

    $Conditions['group'] = "FmtPageName('\$Group',\$pagename)==\$condparm";

One could then use the following markup:

    (:if group PmWiki:) This text only appears in the group "[[PmWiki]]"(:if:)

The negated form of (:if:) is automatically handled as well, thus:

    (:if ! group PmWiki:) This text appears everywhere *but* in group "[[PmWiki]]"(:if:)

Any (:if:) automatically terminates the previous one, thus markup can be easily cased:

    
    (:if group PmWiki:) Do this in [[PmWiki]] group
    (:if group Main:) Do something else for Main
    (:if:) This happens in all groups
    

See also Cookbook:ExpandingMenus for an example to use this to create compact Sidebar menus.

All that remains now is to come up with a good set of $Conditions. Testing for group membership is a good one, we might also want to create something for generic pagename pattern matches

    
    (:if match ^PmWiki\. :) Only appears in the [[PmWiki]] group
    (:if match RecentChanges$ :)  only on [[RecentChanges]] pages
    (:if ! match ^(Main|PmWiki)\. :) All groups except Main and PmWiki
    (:if:)again - all groups
    

There is an (:if auth ...:) conditional markup available for processing depending on the current authorizations in effect. For example, one can create a bullet list with

    
    (:if auth read:)* [[View page -> {$Name}?action=browse]]
    (:if auth edit:)* [[Edit page -> {$Name}?action=edit]]
    (:if auth upload:)* [[Attachments -> {$Name}?action=upload]]
    (:if auth attr:)* [[Page Attributes -> {$Name}?action=attr]]
    (:if auth admin:)* You're logged in as admin
    (:ifend:)
    

and only those items corresponding to the user’s current authorizations will appear. This should be very useful in creating action buttons.

$Conditions could also be conditional on other items; e.g., date or time, day of week, etc.

Default PmWiki Conditions

The following are defined in pmwiki.php, so would not need to be defined in config.php:

    
    $Conditions['false'] = 'false';
    $Conditions['true'] = 'true';
    $Conditions['group'] = "FmtPageName('\$Group',\$pagename)==\$condparm";
    $Conditions['name'] = "FmtPageName('\$Name',\$pagename)==\$condparm";
    $Conditions['match'] = 'preg_match("!$condparm!",$pagename)';
    $Conditions['auth'] =
      '@$GLOBALS["PCache"][$GLOBALS["pagename"]]["=auth"][trim($condparm)]';
    $Conditions['authid'] = '@$GLOBALS["AuthId"] > ""';
    $Conditions['attachments'] = "AttachExist(\$pagename)";
    

Suggested additional condition:

    
    $Conditions['fullname'] = "FmtPageName('\$Group.\$Name',\$pagename)==\$condparm";
    

(This can be achieved with match, but it’s not nearly as clean.)

Samples for if-markups / $Conditions:

(:if group GROUPNAME:) - display following text only, if group = specified groupname:

    $Conditions['group'] = "FmtPageName('\$Group',\$pagename)==\$condparm";

(:if author AUTHORNAME:) - display following text only, if author = specified authorname:

    $Conditions['author'] = "\$GLOBALS['Author']==\$condparm";

will only work, if Author is “logged” on or has already put his name into the author-field while editing a page, i.e. the global variable $Author must be set.

(:if attachments:) - display following text only, if there any attachments to the actual page/group.

(:if enabled VARIABLE:) - display following text only, if VARIABLE defined in config.php or a skin’s php script is set to 1 or some string value. If set to zero (0), or not set, the condition is not true and the following text will not display:

    $Conditions['enabled'] = "(boolean)\$GLOBALS[\$condparm]";

This is useful for instance to have a skin display something, which will be hidden in other skins, by setting a variable in the skin.php file to 1, and checking if the variable is set in the if condition.

Caveat

Q what happens to these kind of things when someone looks at such a page and presses “Show Source”?

 (:if ! auth admin:)
 code seen by other than admins
 (:if auth admin:)
 code seen by admins
 (:if:)

If someone views the HTML source in the browser, then the text
isn’t even there — it’s removed from the output entirely by
the conditional markup.

However, if someone has read permission to the page and uses
?action=source, they’ll see the entire source including the
conditional markups. As per my earlier note, I
think I’m going to take the position that PmWiki’s smallest
atom of security is the page, and not individual pieces of the page.
In other words, (:if …) is a useful way for suppressing parts of
a page but not for protecting it. —Pm?

See also: Cookbook:MultiLanguage

« Special markups | PmWiki.Documentation Index | Markup master index »

Last edited by Klonk.   Page last modified on July 11, 2005

Legal Information |  Designed and built by Emergency Digital. | Hosted by Steadfast Networks