ConditionalExtensions
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: |
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
Note: The recipe is no longer needed to evaluate the boolean expressions described below, since that capability has been integrated into PmWiki core since v2.1.0beta33. (The corresponding code has been removed from the recipe.) However, the recipe's other capabilities are still useful.
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
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.
- 2015-02-17 Dropped obsolete code interfering with PHP5.5 compatibility
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.