[pmwiki-users] Conditional markup with multi-line page variables

Peter & Melodye Bowers pbowers at pobox.com
Mon Jun 30 16:49:30 CDT 2008


> That works exactly as it should, but, if ShopList has a multi-line
> value, I still get the correct output for my shopping list, but it is
> preceded on the screen by the if statement, with ShopList expanded,

The (:if ...:) regex includes a [^\n]* when it is matching the condition
which excludes any newlines being part of your condition.  I messed around
with how that regex might be changed and ran into a problem where the regex
internal to scripts/stdmarkup.php has to be changed as well (needing an s
modifier, if you're interested).  At that point it became easier in my book
to switch over to something non-core, like nested-if.

Try this code in config.php:

===(snip)===
## (:if0:)/(:else0:)/(:if0end:)
$nestedifpat = "/( \\(:if-?(\\w)\\s*\\b(.*?):\\)
     ((?:
        (?:(?!\\(:(?:if|else)).)+
        |(?R)
     )*?)
     (?: \\(: (?:else-?\\2) \\s* :\\)
       ((?:
          (?:(?!\\(:(?:if|else)).)+
          |(?R)
       )*?)
     )?
     (?: \\(: (?:if-?\\2-?end) \\s* :\\) )
    )/seix";
Markup('nestedif', '>if', $nestedifpat, "CondText3(\$pagename, PSS('$3'),
PSS('$4'), PSS('$5'))");

function CondText3($pagename, $condspec, $condtext, $elsetext) {
  global $Conditions;
  #echo
"condspec=$condspec<br>\ncondtext=$condtext<br>\nelsetext=$elsetext<br>\n";
  if (!preg_match("/^\\s*(!?)\\s*(\\S*)\\s*(.*?)\\s*$/s", $condspec,
$match)) 
      echo "ERROR: No Match on condspec \"$condspec\"<br>\n";
  list($x, $not, $condname, $condparm) = $match;
  if (!isset($Conditions[$condname])) return $condtext;
  $tf = @eval("return ({$Conditions[$condname]});");
  if ($tf xor $not) return $condtext;
  else return $elsetext;
}
===(snip)===

And then use (:if0 ...:) instead of (:if ...:), (:else0:) instead of
(:else:) and (:if0end:) instead of (:ifend:).

Here's what my test source looked like:

===(snip)===
(:ShopList: Nuts
* bolts
* washers:)

(:if0 ! equal {$:ShopList} "":)
!!!!Shopping List:
*{$:ShopList}
(:else0:)
NO LIST
(:if0end:)

(:ShopList2::)

(:if0 ! equal {$:ShopList2} "":)
!!!!Shopping List:
*{$:ShopList2}
(:else0:)
NO LIST2
(:if0end:)
===(snip)===

The output looks like this:

===(snip)===
Shopping List:

    * Nuts
    * bolts
    * washers 

NO LIST2
===(snip)===

Note that this is a modified version of nested-if designed to allow
multi-line values.  It seems to work fine in the quick tests I just ran...

-Peter




More information about the pmwiki-users mailing list