Summary: A Conditional Markup extension for PmWiki 2.x
Version: 20061028
Status: Stable
Prerequisites: pmwiki-2.0.beta36
Question answered by this recipe
I've found the ConditionalMarkup very powerful, but how do I handle more complex expressions involving string/number comparisons, pattern/pagename matching and/or several simultaneous conditions ?
Description
Download extendcond.phpΔ, save it in the cookbook directory and add:
include_once("$FarmD/cookbook/extendcond.php");
to your local configuration file.
Notes
The recipe script provides extras conditional markup verbs to handle regular boolean expressions and string/number comparisons.
String/Number comparisons
(:if eq VALUE1 VALUE2:)
(:if lt VALUE1 VALUE2:)
(:if le VALUE1 VALUE2:)
(:if gt VALUE1 VALUE2:)
(:if ge VALUE1 VALUE2:)
| All these conditions compares two string/number values.
They may be either literal or variable values as in: (:if eq {$Author} Dfaure:)
|
Strings containing spaces may be enclosed between quotes.
| condition | meaning |
eq | synonym for equal |
lt | less than |
le | less or equal than |
gt | greater than |
ge | greater or equal than |
All these condition verbs may be given with a trailing 'i' as in eqi, lti,... to do case-insensitives comparisons.
Pattern/Pagename matching
(:if regmatch REG_EXPRESSION VALUE:)
| True if the value matches the regular expression.
|
(:if matchname NAME_SPEC PAGENAME:)
| True if the given pagename/groupname match the wildcard placeholder specification.
|
(:if matchgroup GROUP_SPEC GROUPNAME:)
|
Boolean expressions
The boolean expressions extension defined by this recipe has been integrated into PmWiki core since v2.1.0beta33: The related recipe code part does not interfere with the latest core implementation.
Following forms are equivalents:
(:if expr BOOLEAN_EXPRESSION :)
(:if [ BOOLEAN_EXPRESSION ] :)
(:if ( BOOLEAN_EXPRESSION ) :)
The "expr" condition take sub-conditions expressions combined with regular PHP boolean operators and brackets, and let PHP evaluate them to get the result.
| Example | Name | 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. |
With A and B being either regular conditions (see ConditionalMarkup) or bracketed boolean expressions of regular conditions.
Warnings
- Spaces around operators and brackets are required.
- No specific feedback is given for syntaxic errors or unbalanced brackets.
Sample
With the code below:
(:if expr auth admin || auth attr || auth edit :)
[[Logout -> {$Name}?action=logout]] %green%{$Author}%%
(:if:)
you get a logout link followed by the current author name available only when authentified with higher than 'read' right.
Contributors
See Also
PmWiki.ConditionalMarkup
Release Notes
- 2005-02-28 Initial proof-of-concept release.
- 2005-04-20 Updated to a more reliable/usual syntax.
- 2005-05-03 Following a Pm's suggestion, spaces around operators are now mandatory to allow exclamation marks and parenthesis in regular conditions terms.
- 2005-09-14 Removed extraneous undocumented "Properties" features to its own recipe.
- 2005-09-19 Fixed a security issue (thanks to Pm and M.Fick). Added alternates syntax forms and equality condition.
- 2006-03-02 Revamped equality condition to more general string/numbers comparisons. Updated licensing terms.
- 2006-06-20 Added partial string matches with regular expressions.
- 2006-10-28 Added RecipeInfo data. Added pagename wildcard matching.
Sandbox
Some boolean tables:
A | B | C | A or ( B and C ) | A and ( ! B or C ) |
| F | F | F | F | F |
| F | F | T | F | F |
| F | T | F | F | F |
| F | T | T | T | F |
| T | F | F | T | T |
| T | F | T | T | T |
| T | T | F | T | F |
| T | T | T | T | T |
A | B | ( A && ! B ) || ( ! A && B ) | A xor B |
| F | F | F | F |
| F | T | T | T |
| T | F | T | T |
| T | T | F | F |
Comments
See discussion at ConditionalExtensions-Talk
User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.