Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

ConditionalMarkupSamples

Summary: List of default and custom conditional markup definitions
Version: 2006-03-02
Status: Stable
Prerequisites: pmwiki-2.0
Maintainer:
Categories: Markup
Votes:

(:if key arguments:) samples and definitions

PmWiki comes with a number of default conditions already defined. See Conditional Markup and also below. Other conditions can be added by defining them in local/config.php or another local customisation file.

Conditionals not defined in PmWiki

To use any of the following conditionals you need to add the definition to your config.php or another local configuration file.

if action Not defined in PmWiki

Displays following text only if a given action is in progress.

  • Markup: (:if action ACTION:) (to check if ?action=ACTION is happening)
  • Definition: $Conditions['action'] = '\$GLOBALS["action"]==\$condparm';
  • Example: Add a 'View' item to Site.PageActions that only displays when we aren't currently performing that action.
    
      (:if ( ! action browse ) :)
      * %item class=browse accesskey='$[ak_view]'%[[{$FullName} | $[View] ]]
      (:ifend:)

if attachexists Not defined in PmWiki

Displays following text only if the given file exists as an attachment. The single argument can be in the form "filename.ext", "directory/filename.ext" or "directory/pagename/filename.ext".

  • Markup: (:if attachexists filename.jpg:)
  • upload files in Group/ subfolders:
    (:if attachexist Group/myfile.doc:)
  • upload files in Group/PageName subfolders:
    (:if attachexist Group/PageName/myfile.doc:)
Definition:

$Conditions['attachexists'] = 'UploadFileExist($pagename, $condparm)';
function UploadFileExist($pagename, $attachname) {
global $UploadDir, $UploadPrefixFmt;

        $fname = explode("/", $attachname);
        $filename = end($fname);
        if(count($fname)==2) 
                $pagename = MakePageName($pagename, $fname[0]);
        if(count($fname)==3) 
                $pagename = MakePageName($pagename, $fname[0].".".$fname[1]);
        $uploaddir = FmtPageName("$UploadDir$UploadPrefixFmt", $pagename);
        $dirp = @opendir($uploaddir);
        if (!$dirp) return '';
        $filelist = array();
        while (($file=readdir($dirp)) !== false) {
                if ($file{0} == '.') continue;
                        $filelist[$file] = $file;
        }
        closedir($dirp);
        return in_array($filename, $filelist);

}

Alternative implementation

Displays following text only if the given file exists as an attachment. The single argument can be in the form "filename.ext", "Group/filename.ext" or "Group.Page/filename.ext".

The difference between this and the above is that it's independent of your uploads folder structure, as the part of the argument before the "/" is taken as a page name; if the attachment you're looking for would be found if you wrote Attach:filename.ext on that page, it'll exist with this function as well. The default is to use the current page.

Markup: (:if attachexists filename.jpg:) or (:if attachexists Page/filename.jpg:).
Definition:
$Conditions['attachexists'] = "AttachFileExists(\$pagename,\$condparm)";
function AttachFileExists( $pagename, $path ) {
	global $UploadFileFmt;
	if (preg_match('!^(.*)/([^/]+)$!', $path, $match)) {
		$pagename = MakePageName($pagename, $match[1]);
		$path = $match[2];
	}
	$upname = MakeUploadName($pagename,$path);
	$filepath = FmtPageName("$UploadFileFmt/$upname", $pagename);
	return file_exists($filepath);
}

if author Not defined in PmWiki

a) Displays following text only if author = specified authorname.

  • Markup: (:if author AUTHORNAME:)
  • Definition: $Conditions['author'] = "\$GLOBALS['Author']==\$condparm";

b) Displays following text only, if author within specified list of authors.

  • Markup: (:if author AUTHORNAME:) (for a single author)
  • Markup: (:if author AUTHORNAME,AUTHORNAME,...:) (for an autorlist)
  • Definition: $Conditions['author'] = 'preg_match("/".$GLOBALS["Author"]."/",$condparm)';

This 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. You can use only one of this author-condition-definition

if authgroup Not defined in PmWiki

Displays following text only if the viewer is currently authenticated in the given group. (ie. logged in, and a member of the given authorization group) via optional AuthUser authentication scheme.

  • Markup: (:if authgroup @groupname:) or even (if authgroup id:sally)
  • Definition: $Conditions['authgroup'] = '$GLOBALS["AuthList"][$condparm] > 0';

if authuser Not defined in PmWiki

Displays following text only if the viewer is currently logged in with username 'NAME' via the optional AuthUser authentication module. Do not confus ethis with PmWiki's (:if authid:), which returns true if user is logged in, but does not check the user name!

  • Markup: (:if authuser NAME:)
  • Definition: $Conditions['authuser'] = '\$GLOBALS["AuthId"]==\$condparm';

if fullname Not defined in PmWiki

Displays following text only if current fullname = specified fullname (GROUP.NAME).

  • Markup: (:if fullname GROUPNAME.PAGENAME:)
  • Definition: $Conditions['fullname'] = "FmtPageName('\$Group.\$Name',\$pagename)==\$condparm";

You can also use (:if equal "{*$FullName}" "GROUPNAME.PAGENAME":)

if handler Not defined in PmWiki

Displays following text only if a given action is defined. This differs from (:if action ACTION:) which only includes the text if the action is currently being executed. Note that this doesn't work for the diag and phpinfo actions, as they don't use the HandleActions dispatcher. Test for them with (:if enable EnableDiag:) instead.

  • Markup: (:if handler ACTION:) (to check if ACTION is a known action)
  • Definition: $Conditions['handler'] = '(boolean)@$GLOBALS["HandleActions"][$condparm]';
  • Example: Add a 'Del' item to Site.PageActions that only displays if the DeleteAction recipe is loaded.
    
      (:if handler delete:)
      * %rel=nofollow% [[{$Name}?action=delete| $[Del] ]]
      (:ifend:)

if intext Not defined in PmWiki

Displays following text only if a given string exists in the page text. It does not look in markup directives of form (:...:). The optional second argument can be a page name, to check for presence of 'string' in that page, instead of the current page. Enclose any string which contain spaces in quotes: 'string with spaces'.

  • Markup: (:if intext 'string':) or (:if intext 'string' Group.PageName:)
Definition:
# add (:if intext 'string':) conditional
$Conditions['intext'] = 'StringInText( $pagename, $condparm )';
function StringInText( $pn, $arg ) {
	$arg = ParseArgs($arg);
	if($arg[''][1]) $pn = MakePageName($pn, $arg[''][1]);
	$page = RetrieveAuthPage($pn, 'read', true);
	$text = preg_replace('/\\(:(.*?):\\)/' ,"", $page['text']);
	if( strpos($text, $arg[''][0])!==false ) return true;
}

if recipe-loaded Not defined in PmWiki

Displays following text only if a given cookbook recipe is in use. The argument should be the name the recipe uses of itself, not its filename. This is most probably the same as its page name on pmwiki.org, but may also be found in the file itself: look for a row of text near the beginning that looks like $RecipeInfo['RECIPENAME']['Version'] = 'xxxx';

  • Markup: (:if recipe-loaded name:)
  • Definition: $Conditions['recipe-loaded'] = "isset(\$GLOBALS['RecipeInfo'][\$condparm])";

Conditionals already defined in PmWiki

if attachments Defined in PmWiki

Displays following text only if current page/group has any attachments.

  • Markup: (:if attachments:)
  • Definition: $Conditions['attachments'] = "AttachExist(\$pagename)";

if auth Defined in PmWiki

Displays following text only if user has authenticated during the current browser session

  • Markup: (:if auth ACTION:)
    • (:if auth read:)
    • (:if auth edit:)
    • (:if auth attr:)
    • (:if auth admin:)
  • Definition: $Conditions['auth'] = '@$GLOBALS["PCache"][$GLOBALS["pagename"]]["=auth"][trim($condparm)]';

if authid Defined in PmWiki

Displays following text only if user is authenticated, i.e. logged in with a valid user name and password via optional AuthUser authentication scheme. It does not check particular user names!

  • Markup: (:if authid:)
  • Definition: $Conditions['authid'] = '@$GLOBALS["AuthId"] > ""';

if date Defined in PmWiki

Displays following text only if current date matches date given or matches range given. Date or range given as 8 number datestring in form of yyyy-mm-dd or yyyymmdd. Range includes first and last date given.

  • Markup: (:if date DATE:) or (:if date DATE1..DATE2:)
  • Definition: $Conditions['date'] = "CondDate(\$condparm)";

defined as special function in scripts/stdmarkup.php.

if enabled Defined in PmWiki from pmwiki 2.1 beta1

Displays following text only, if VARIABLE is 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.

  • Markup: (:if enabled VARIABLE:)
  • Definition: $Conditions['enabled'] = '(boolean)@$GLOBALS[$condparm]';

One use for skins: in order for a skin to display something, which will be hidden in other skins, a variable can be set in the skin.php file to 1, and the 'if enabled' markup checks if the variable is set.

if equal Defined in PmWiki from version 2.1.beta15

Displays following text only if string1 equals string2

  • Markup: (:if equal STRING1 STRING2:)
  • Definition: $Conditions['equal'] = 'CompareArgs($condparm) == 0';
    function CompareArgs($arg) {
      $arg = ParseArgs($arg); return strcmp(@$arg[''][0], @$arg[''][1]);
      }
    

if exists Defined in PmWiki

Displays following text only if SomeGroup.SomeName exists.

  • Markup: (:if exists SomeGroup.SomeName:)
  • Definition: $Conditions['exists'] = 'PageExists(MakePageName(\$pagename, \$condparm))';

if false Defined in PmWiki

This condition is always false, following text including markup will not be displayed.

  • Markup: (:if false:)
  • Definition: $Conditions['false'] = 'false';

if group Defined in PmWiki

Displays following text only if current group = specified groupname.

  • Markup: (:if group GROUPNAME:)
  • Definition: $Conditions['group'] = "FmtPageName('\$Group',\$pagename)==\$condparm";

if match Defined in PmWiki

Displays following text only if current page matches the regular expression.

  • Markup: (:if match REG_EXPRESSION:)
  • Definition: $Conditions['match'] = 'preg_match("!$condparm!",$pagename)';

if name Defined in PmWiki

Displays following text only if current name = specified pagename.

  • Markup: (:if name PAGENAME:)
  • Definition: $Conditions['name'] = "FmtPageName('\$Name',\$pagename)==\$condparm";

if ontrail Defined in PmWiki

Displays following text only if page is on trail page specified.

  • Markup: (:if ontrail PAGENAME:)

(defined in scripts/trails.php)

if true Defined in PmWiki

This condition is always true, following text including markup will always be displayed.

  • Markup: (:if true:)
  • Definition: $Conditions['true'] = 'true';

Note

Do not use conditional markup to hide security sensitive content. Although portions of a page can be suppressed, they cannot be protected. You will need to protect the entire page instead.

-- Instead, consider creating the security-sensitive content in a separate page and including it, while security that other page. BenWilson January 20, 2006, at 11:52 AM

If someone has read permission to the page and uses ?action=source, they'll see the entire source including the conditional markups.

  • So, then set the 'source' permissions to the 'edit' level. This will mean that source is only viewable to those who can edit the page, and read-only permissions are insufficient to view source. BenWilson April 12, 2007, at 08:37 AM

Please read http://thread.gmane.org/gmane.comp.web.wiki.pmwiki.user/30109/focus=30122 if you have problems with (:if action...:)
Luigi 13 September 2006

See also

Q and A

What are the "If Auth" equivalents for a skin.php file?

if (CondAuth($pagename,'read')) /* visitor has read permission */
if (CondAuth($pagename,'edit')) /* visitor has edit permission */
if (CondAuth($pagename,'attr')) /* visitor has attr permission */
if (CondAuth($pagename,'admin')) /* visitor has admin permission */

Is there a way to detect if the current page is part of the sidebar? I'd like to have something like:

*Menu1
(:if !sidebar:)
**SubMenu1
**SubMenu2
(:if:)
*Menu2
(:if !sidebar:)
**SubMenu1
**SubMenu2
(:if:)

which would be included by sidebar.

Can you not use (:if name.... :) somehow? - But i may not understand the question. What do you mean by: "if the current page is part of the sidebar?" HansB

(:if name NAME:) evaluates to true if the main page loaded is NAME (for pages in both the main window or sidebar). What I want is when a page is loaded in the main window, it will display the entire menu. When that same page is included by another page (specifically sidebar), it will only display major items (no matter what the main window is displaying).

How I got to this was by ExpandingMenus. But instead of a SideBar-menu page, I'd like to include a detailed page, but in the sidebar hide the details. That way I don't need to update two pages to point to new content.

I maybe crafting a problem to a solution I want. Is there a better way to do something like expanding menus without modifying two pages (Sidebar-menu and a group homepage)?

One way you may try is this: add to the main page special tags for all the lines or part of lines you don't want to see if this page gets loaded in the sidebar, a tag like %sbhide% may do. You can also use >>sbhide<< ...section to hide in sidebar... >><< to hide whole sections of the page. So you write:
          *Menu1
          >>sbhide<<
          **SubMenu1
          **SubMenu2
          >><<
          *Menu2
          >>sbhide<<
          **SubMenu1
          **SubMenu2
          >><< 
Then to make it work you would need to set a css style which is only relevant for this class sbhide if it is part of the sidebar. Usually #sidebar .sbhide {display:none} should work, but you may need to check your skin to see if the sidebar is in a div called #sidebar. Set the style in the skin's css file, or pub/css/local.css, or via the $HTMLStylesFmt[] variable in config.php.
You can also specify in the include markup you use in the sidebar which parts of the main page shall be included, by using anchors or specific line numbers. See PmWiki.IncludeOtherPages. ~HansB
Thank you. I like the css method. I had hacked pmwiki.php to have a wiki variable IsIncludePage so I could do it with (:if:). It's not a good way unless/until it got moved into mainline. Think there is any value to this variable?

Uhm... How do I (:if name Page With Spaces:)?

No such thing, all page names (not to be confused with page titles) are WikiWords, spaces and non 'wiki work characters' are removed from them, see also Links.

Ah, you're right. My configuration keeps underscores in page names, so something like (:if name Page_With_Spaces:) works. I guess what I really wanted is an (:if title Page Title With Spaces:)... Thanks!
You can use (:if equal "{*$Title}" "Page Title With Spaces":). --Petko February 16, 2007, at 09:46 AM

Can I ask (:if some-cookbook-recipe-has-been-loaded:)?

See above. EemeliAro

Can I test (:if skin SKINNAME:) to determine the skin that is being used?

Please check the 'if enabled' section above.

Is there a way to reference a certain attachment to check whether it exists or not? ~LasseS April 29, 2007

See above. EemeliAro

Not yet. --Petko

Can I apply multiple arguments?

Would like the sidebar to display items when the name is NOT Website.* or Holiday.* Example: (:if name [-Website.* && -Holiday.* ] :)This is NOT Website OR Holiday (:ifend:) With a single argument it works fine. Any ideas? 6-aug-07
A: This one you can do like (:if ! group Website,Holiday:). For other, more complicated, use expr like this: (:if expr [ ! group Category && auth edit ] :) You can have brackets and inside them, parentheses if the expression is more complex. --Petko

See Also

Contributors


Category: Markup
Edit - History - Print - Recent Changes - Search
Page last modified on June 22, 2008, at 10:33 AM