PHx-Modifier: thenc/elsec

von Thomas Jakobi am Sonntag, 16. Mai 2010 um 10:08 Uhr.

PHx then- und else-Modifier haben eine dumme Eigenschaft: Sie werden immer ausgeführt, und je nach logischem Zustand des Ausdrucks wird die Ausgabe unterdrückt. Dies ruft z.B. dann ein Problem hervor, wenn das Snippet $modx->regClientStartupScript ausführt. Die folgenden Modifier lösen das Problem.

Voraussetzungen

Folgende Plugins/Snippets müssen installiert und funktionsfähig sein:

PHx

Es werden zwei Dateien im Verzeichnis assets/plugins/phx/modifiers angelegt und mit folgendem Inhalt bestückt:

thenc.phx.php
<?php
/* 
 * description: Returns the content of a chunk if a phx expression is true
 * reason:      PHx has one big problem with 'then' or 'else' constructs because the modx-parser 
 *              inserts all (visible) chunks at the beginning of the parsing process. 
 *              PHx parses then snippets and the phx-logic from inside to outside 
 *              (i.e. snippet-calls inside of snippets-parameters were executed first).
 *              Also every snippet in the then or else branch will be executed even if the
 *              phx expression is not hit - only the output is surpressed.
 *              This modifier solves this.
 * usage:       [+phx:if=`condition`:eq=`condition`:thenc=`chunkname`+]
 */
 
global $modx;
 
$conditional = implode(' ', $condition);
$isvalid = intval(eval("return (" . $conditional . ");"));
if ($isvalid) {
    $output = $modx->getChunk($options);
}
return $output;
elsec.phx.php
<?php
/* 
 * description: Returns the content of a chunk if a phx expression is false
 * reason:      PHx has one big problem with 'then' or 'else' constructs because the modx-parser 
 *              inserts all (visible) chunks at the beginning of the parsing process. 
 *              PHx parses then snippets and the phx-logic from inside to outside 
 *              (i.e. snippet-calls inside of snippets-parameters were executed first).
 *              Also every snippet in the then or else branch will be executed even if the
 *              phx expression is not hit - only the output is surpressed.
 *              This modifier solves this.
 * usage:       [+phx:if=`condition`:eq=`condition`:elsec=`chunkname`+]
 */
 
global $modx;
 
$conditional = implode(' ', $condition);
$isvalid = intval(eval("return (" . $conditional . ");"));
if (!$isvalid) {
    $output = $modx->getChunk($options);
}
return $output;
?>



Bislang habe ich folgende PHx Modifier veröffentlicht: