(:Summary:The if directive allows portions of a page to be included or excluded from rendering:) %define=indent block margin-left=2em% (:Audience: authors, admins (advanced) :) !! Using the [=(:if:)=] Directive The [@(:if:)@] directive allows portions of a page to be included or excluded from rendering. The generic forms of the [@(:if:)@] directive are -> [@(:if cond param:) body (:ifend:)@] -> [@(:if cond param:) body (:else:) body (:ifend:)@] -> [@(:if cond param:) body (:elseif cond param:) body (:ifend:)@] '^see [[PmWiki/ReleaseNotes#elseifelse|#]] [[PmWiki/ChangeLog#beta32|#]]^' where "cond" names a [[#built-in-conditions | condition to be tested]], and "param" is a parameter or other argument to the condition. !! Markup Shortcut You can also use an abbreviated form of [@(:ifend:)@], [@(:if:)@] for brevity: -> [@(:if cond1:) cond1 is true (:if:)@] -> [@(:if cond1:) cond1 is true (:if cond2:) cond2 is true (:if:)@] The latter is identical to : -> [@(:if cond1:) cond1 is true (:ifend:)(:if cond2:) cond2 is true (:ifend:)@] !! Built-in Conditions [[#built-in-conditions]] The built-in conditions include: (:table class='indent' border=0 cellpadding=0 cellspacing=0 valign=top:) (:cellnr:)[@(:if name PAGENAME:)@] (:cell:) -  (:cell:)current page is named "[@PAGENAME@]" (:cellnr:)[@(:if group GROUPNAME:)@] (:cell:) -  (:cell:)current group is named "[@GROUPNAME@]" (:cellnr:)[@(:if auth LEVEL PAGENAME:)@] (:cell:) -  (:cell:)viewer is authorized - meaning "what they are allowed to do" - matches a "[@LEVEL@]" where [@LEVEL@] can be: [@read@], [@edit@], [@upload@], [@attr@] or [@admin@]; [@PAGENAME@] is optional. (:cellnr:)[@(:if authid:)@] (:cell:) -  (:cell:)current viewer is authenticated - meaning they have proven who they are via login - to use this the wiki must include recipe [[PmWiki/AuthUser|AuthUser]] or others which set the [@$AuthId@] variable. (:cellnr:)[@(:if enabled InvalidLogin:)@] (:cell:) -  (:cell:)username and password not authenticated (:cellnr:)[@(:if true:)@] (:cell:) -  (:cell:)always include text (:cellnr:)[@(:if false:)@] (:cell:) -  (:cell:)always exclude text (same as a comment) (:cellnr:)[@(:if attachments:)@] (:cell:) -  (:cell:)current page has one or more attachments (:cellnr:)[@(:if date DATE VALUE:)@] (:cell:) -  (:cell:)[@DATE@] may be year-month. year-month-day is optional. Evaluates to true if [@VALUE@] is within [@DATE@] ("now" or "today" is assumed for VALUE. VALUE may be omitted, as in the following examples.) (Note that [@VALUE@] can be a recognizable date via strtotime() whereas DATE [or DATE1 and DATE2 below] have a more fixed format which explicitly must exclude spaces. Any spaces in DATE1 or DATE2 cause unpredictable results.) (:cellnr:)[@(:if date DATE..:)@] (:cell:) -  (:cell:)true if current date is [@DATE@] or later (unlimited) (:cellnr:)[@(:if date DATE1..DATE2:)@] (:cell:) -  (:cell:)true if current date is in range [@DATE1@] to [@DATE2@] (inclusive) %green newwin%[-''dates are in [[http://w3.org/QA/Tips/iso-date|standard]] format yyyy-mm-dd or yyyymmdd or yyyymmddhhmm (but see comment above on format of VALUE)''-] [[<<]][-Note the ".." cannot have leading or trailing spaces.-] (:cellnr:)[@(:if enabled VAR:)@] (:cell:) -  (:cell:)true if PHP VAR is true (:cellnr:)[@(:if enabled AuthPw:)@] (:cell:) -  (:cell:)true if user has entered any password during the current browser session. - This does not mean the user has entered the correct password, just that they entered one. (:cellnr:)[@(:if equal STRING1 STRING2:)@] (:cell:) -  (:cell:)true if [@STRING1@] equals [@STRING2@], use quotes if the string or string variable contains spaces, eg [@"MY STRING"@] (:cellnr:)[@(:if match REG_EXPRESSION:)@] (:cell:) -  (:cell:)true if current page name matches the regular expression (:cellnr:)[@(:if exists PAGENAME:)@] (:cell:) -  (:cell:)true if the page ''pagename'' exists (:cellnr:)[@(:if action ACTION:)@] (:cell:) -  (:cell:)true if the action ?action=ACTION (edit, print, ...) is currently permitted. To test what the current action being requested is, use [@(:if equal {$Action} ACTION:)@]. (:cellnr:)[@(:if ontrail WikiTrailPage ThisPage:)@] (:cell:) -  (:cell:)true if ThisPage is in a list used as a [[wiki trails | trail]] on WikiTrailPage (:tableend:) The name and group conditionals will work even for an included page, as the "name" and "group" conditionals always check the currently displayed page, as opposed to the page that the markup appears in. !! Negated Conditions Negated forms of conditions also work: (:table class='indent' border=0 cellpadding=0 cellspacing=0 valign=top:) (:cellnr:)[@(:if !attachments:)@] (:cell:) -  (:cell:)this page has no attachments (:cellnr colspan=3:)  (:cellnr:)[@(:if ! name PAGENAME:)@] (:cell rowspan=3:) (:cell rowspan=3 valign=middle:)current page is NOT named "[@PAGENAME@]" (:cellnr:)[@(:if name -PAGENAME :)@] (:cell:) (:cellnr:)[@(:if name !PAGENAME :)@] (:tableend:) !! Nesting Conditions [[#nested-conditions]] Conditions may be nested (from 2.2.beta 66). Nested [@(:if:)@] works the same way as nested [@(:div:)@]. To have nested conditionals you need to number the if, and the matching else/ifend: ->[@ (:if cond1:) cond1 is true (:if2 cond2:) cond1 and cond2 are true (:else2:) cond1 is true, cond2 is not (:if2end:) (:else:) cond1 is false, cond2 testing was skipped (:ifend:) @] [-''Spaces were added for better readability.''-] !! Using wildcard placeholders(:if false:)%green%[-(new for pmwiki 2.1.beta21)-]:(:ifend:) The character [@*@] can be used as a wildcard to represent any character, zero, one, or multiple times.\\ The character [@?@] can be used as a wildcard to represent any character exactly once.\\ Wildcard characters ([@*@] and [@?@]) can be used with the ''name'' and ''group'' conditional markups, thus: (:table class='indent' border=0 cellpadding=0 cellspacing=0 valign=top:) (:cellnr:)[@(:if name PmCal.2005* :)@] (:cell:) -  (:cell:)current page is in group PmCal and begins with 2005 (:cellnr:)[@(:if group PmWiki* :)@] (:cell:) -  (:cell:)current page is in group PmWiki or a group beginning with PmWiki (:cellnr:)[@(:if name Profiles.*,-Profiles.Profiles :)@] (:cell:) -  (:cell:)current page is in group [@Profiles@] but not [@Profiles.Profiles@] (:tableend:) !!Using [[PmWiki/page text variables]], [[PmWiki/page variables]] and [[PmWiki/markup expressions]] Page text variables (PTVs), page variables (PVs) and markup expressions can be used in conditional markup. They will be assigned/evaluated before the condition(s). !! Use with [[PmWiki/page list templates]] Conditional markup is used extensively with [[Site/page list templates]]. Use with page variables: -> = current item -> < previous item -> > next item Conditionals used to structure pagelist output: [@(:if equal {<$Group}:)@] deprecated in favour of [@(:template first:)@] At beginning of list [@(:if equal {>$Group}:)@] deprecated in favour of [@(:template last:)@] At end of list [@(:if ! equal {=$Group} {<$Group}:)@] deprecated in favour of [@(:template first {=$Group}:)@] First item in group [@(:if ! equal {=$Group} {>$Group}:)@] deprecated in favour of [@(:template last {=$Group}:)@] Last item in group !! Combining conditions Conditions (as previously defined) may be combined into more complex conditional expressions using one of these three equivalent forms: ->[@ (:if expr EXPRESSION :) (:if [ EXPRESSION ] :) (:if ( EXPRESSION ) :)@] Conditions are combined into expressions with boolean operators and brackets. In the next table, A and B are either regular conditions or (round-)bracketed sub-expressions of regular conditions: >>indent<< || border=1 cellpadding=2 cellspacing=0 ||! Expression ||! Operator ||! Result || || [@A and B@] || And ||TRUE if both A and B are TRUE.|| || [@A or B@] || Or ||TRUE if either A or B is TRUE.|| || [@A xor B@] || Xor ||TRUE if either A or B is TRUE, but not both.|| || [@! A@] || Not ||TRUE if A is not TRUE.|| || [@A && B@] || And ||TRUE if both A and B are TRUE.|| || [@A || B@] || Or ||TRUE if either A or B is TRUE.|| >><< Example ->[@ (:if [ name SomePage and group SomeGroup ]:) equivalent to (:if name SomeGroup.SomePage:)@] Note: * Spaces around operators and brackets are required. * No specific feedback is given for syntax errors or unbalanced brackets. * Use round brackets (not square) for nested expressions. Thus, the following is a valid way of building an expression that shows the following contents only when the user is either the administrator, or is logged in and the time is later than the given date: ->[@(:if [ auth admin || ( authid && date 2006-06-01 ) ] :)@] Nesting with square brackets will silently fail to work as expected: ->[@(:if [ auth admin || [ authid && date 2006-06-01 ] ] :) @]   %red%NOTE: Doesn't Work! A common use of these complex tests are for expressions like: ->[@(:if expr auth admin || auth attr || auth edit :)@] ->[@[[Logout -> {$Name}?action=logout]]@] ->[@(:ifend:)@] which provides a ''logout'' link only when the browser has admin, attr, or edit permissions. %audience% admins (advanced) !! Creating new conditions See [[Cookbook:ConditionalMarkupSamples]]. See also [[PmWiki/PageVariables#specialreferences|special references]] for the use of [={*$Variables}=]. >>faq<< [[#faq]]