authors, admins (advanced)
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:) (:if cond param:) body (:elseif cond param:) body (:else:) body (:ifend:) |
where "cond" names a condition to be tested, and "param" is a parameter or other argument to the condition.
Note that (:if:)
without parameters and (:ifend:)
are identical. Also note that (:if cond:)
automatically closes a previous conditional. For nested multiple levels, see Nested conditionals.
The built-in conditions include:
(:if name PAGENAME:)
PAGENAME
" or "GROUPNAME.PAGENAME
"
(:if group GROUPNAME:)
GROUPNAME
"
(:if auth LEVEL PAGENAME:)
LEVEL
" where LEVEL
can be: read
, edit
, upload
, attr
or admin
; PAGENAME
is optional.(:if auth @readers,@editors:)
(:if authid:)
$AuthId
variable.
(:if enabled InvalidLogin:)
(:if true:)
(:if false:)
(:if attachments FILENAMES PAGENAME:)
PAGENAME
has one or more attachments among the specified. A pagename can be omitted, in that case the current page is implied.FILENAMES
specify an attachment like "pic1.jpg" or attachment patterns separated by commas, like "pic*.jpg,*.png" where asterisk (*) means "anything"; if omitted, any attachment (i.e. "*") is implied. PAGENAME
is not specified, the condition applies to the main page. (:if attachments *.png,*.gif Groupname.PageName:)
(FILENAMES must not have quotation marks)
In the following "if date" examples:
DATE
may be year-month. year-month-day is optional.
VALUE
can be a recognizable date via strtotime()
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
VALUE
is omitted
yyyy-mm-dd
or yyyymmdd
or yyyymmddThhmm
(note the "T" between the date and the hour, and also see comment above on format of VALUE
)
DATE1
) or trailing spaces (when used with DATE2
)
(:if date DATE VALUE:)
VALUE
is within DATE
(:if date DATE1.. VALUE:)
VALUE
(or current date if omitted) is DATE1
or later (unlimited)
(:if date ..DATE2 VALUE:)
VALUE
(or current date if omitted) is DATE2
or earlier (unlimited)
(:if date DATE1..DATE2 VALUE:)
VALUE
(or current date if omitted) is in range DATE1
to DATE2
(inclusive)
(:if enabled VAR:)
VAR
exists and is not false
(:if enabled AuthPw:)
(:if equal STRING1 STRING2:)
STRING1
equals STRING2
, use quotes if the string or string variable contains spaces, eg "MY STRING"
(:if match REG_EXPRESSION:)
(:if exists PAGENAME:)
pagename
" or "groupname.pagename
" exists (case insensitive)(:if exists p1,p2,mygroup.*:)
is true if at least one among the pages "p1", "p2" or those in the "mygroup" group exist.
(:if ontrail WikiTrailPage ThisPage:)
ThisPage
is in a list used as a trail on "WikiTrailPage
"
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.
Note: Although there is no built-in conditional markup to test ?action=, you can use (:if equal {$Action} ACTION:)
to test what the current action being requested is.
In some cases where built in conditions have a parameter the parameters may be concatenated using a comma, viz:
(:if name Name1,Name2,-Name3:)
(:if group -Group1,Group2,Group3:)
Negated forms of conditions also work:
(:if !attachments:)
(:if ! name PAGENAME:)
(:if name -PAGENAME :)
PAGENAME
"
(:if group -GROUPNAME1,-GROUPNAME2 :)
GROUPNAME1
" or "GROUPNAME2
"
Note that (:if cond:)
automatically closes a previous conditional. Thus, the following two examples have identical meaning:
(:if cond1:)
cond1 is true (:if cond2:)
cond2 is true (:ifend:)
(:if cond1:)
cond1 is true (:ifend:)
(:if cond2:)
cond2 is true (:ifend:)
Conditions can be nested from 2.2.beta 66. 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 (:elseif2 cond3:) cond1 and cond3 are true, cond2 is not (:else2:) cond1 is true, cond2 and cond3 are not (:if2end:) (:else:) cond1 is false, cond2 testing was ignored (:ifend:) |
Spaces were added for better readability.
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:
(:if name PmCal.2005* :)
(:if group PmWiki* :)
(:if name Profiles.*,-Profiles.Profiles :)
Profiles
but not Profiles.Profiles
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).
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:
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:) |
Important Notes:
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 ] ] :)
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.
admins (advanced)
See Cookbook:ConditionalMarkupSamples.
See also special references for the use of {*$Variables}
.
This page may have a more recent version on pmwiki.org: PmWiki:ConditionalMarkup, and a talk page: PmWiki:ConditionalMarkup-Talk.