[TYPO3-dev] Conditions in Templates
Elmar Hinz
elmar.DOT.hinz at team.MINUS.red.DOT.net
Sun Jan 1 20:12:28 CET 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Here is the code, for the curious. It is still alpha.
/**
* Use or remove subparts with ISSET_var patterns
* Example of usage see below.
*/
function substituteMarkers($subpart, $markers){
foreach(split(chr(10), $subpart) as $line){
// works only on it's own line
$pattern = '/###ISSET_+([^#]*)_*###/';
// set for odd ISSET_xyz, else reset
if(preg_match($pattern, $line, $matches)) {
if(!$flags[$matches[1]]) { // Setzen
$flags[$matches[1]] = true;
// set nowrite flag if required until the next ISSET_xyz
// (only if not already set by envelop)
if( (!$this->markersCountAsSet($markers, $matches[1]) )
&& (! $nowrite ) ) $nowrite = $matches[1];
}else{ // close it
$flags[$matches[1]] = false;
if($nowrite == $matches[1]) $nowrite = 0;
}
} else { // It is no ISSET_line. Write if permission is given.
if($nowrite == false) $out .= $line;
}
}
$out = $this->cObj->substituteMarkerArrayCached($out, $markers);
return $out;
}
/**
* If markers count as set depends on && or || of marker condition
*/
function markersCountAsSet($markers, $conditionValue) {
// Find first || or &&
$pattern = '/_*([A-Za-z0-9]+)_*(\|\||&&)_*([^_]+)_*/';
// recurse if there are more
if( preg_match($pattern, $conditionValue, $matches) ){
$isset = isset($markers['###' . $matches[1] . '###']);
if($matches[2] == '||' && $isset)
$return = true;
elseif($matches[2] == '||' && !$isset)
$return = $this-> markersCountAsSet($markers, $matches[3]);
elseif($matches[2] == '&&' && $isset)
$return = $this-> markersCountAsSet($markers, $matches[3]);
elseif($matches[2] == '&&' && !$isset)
$return = false;
else
debug("Invalid format of ISSET Marker. Hint: " . $matches[2] );
} else {
// remove underscores
$pattern = '/_*/';
$str = preg_replace($pattern, $str, '');
// end of recursion
$return = isset($markers['###' . $conditionValue . '###']);
}
return $return;
}
Example template:
<!---- ###ISSET_projectBegin_||_projectEnd### -->
<p> Laufzeit:
<!-- ###ISSET_projectBegin### -->
von ###projectBegin###
<!-- ###ISSET_projectBegin### -->
<!-- ###ISSET_projectEnd### -->
bis ###projectEnd###
<!-- ###ISSET_projectEnd### -->
</p>
<!---- ###ISSET_projectBegin_||_projectEnd### -->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFDuCmcO976RNoy/18RAusjAJ9A+thvnTWfCJEZCC5xrIcmHe9+FACfZC16
Q8k4GygGpqY3EeI7mvJdixQ=
=/7pG
-----END PGP SIGNATURE-----
More information about the TYPO3-dev
mailing list