103 lines
3.3 KiB
PHP
Executable File
103 lines
3.3 KiB
PHP
Executable File
<?php #lib/20_template.php
|
|
|
|
/********************************************************************************
|
|
* Content: Template-System *
|
|
* Author: Nils Otterpohl *
|
|
* Last modification: 21.07.2010 *
|
|
* Version: final (complete, tested, commented) *
|
|
********************************************************************************/
|
|
|
|
function tplLoadFile($tpl)
|
|
{ //returns a template
|
|
if (file_exists($tpl)) {
|
|
return file_get_contents($tpl);
|
|
} else if (file_exists($tpl.".html")) {
|
|
return file_get_contents($tpl.".html");
|
|
} else {
|
|
addError("noTpl", $tpl);
|
|
return "";
|
|
}
|
|
}
|
|
|
|
function tplCheckMarker(&$marker)
|
|
{ //ensures correctness of marker
|
|
while (!(substr($marker,0,3)=="###")) $marker = "#".$marker;
|
|
while (!(substr($marker,-3,3)=="###")) $marker = $marker."#";
|
|
}
|
|
|
|
function tplMakeSectionMarker($marker, $pos)
|
|
{ //creates START or END marker of section
|
|
$ret = "<!-- " . $marker . " " . $pos . " -->";
|
|
return $ret;
|
|
}
|
|
|
|
function tplExtrSection($tpl, $section)
|
|
{ //Extracts a section from a template
|
|
tplCheckMarker($section);
|
|
$start = tplMakeSectionMarker($section, "START");
|
|
$end = tplMakeSectionMarker($section, "END");
|
|
$start = strpos($tpl, $start)+strlen($start);
|
|
$end = strrpos($tpl, $end);
|
|
if (($start===false) || ($end===false) || ($end<=$start)) return "";
|
|
return substr($tpl, $start,$end-$start);
|
|
}
|
|
|
|
function tplReplSection($tpl, $section, $value)
|
|
{ //replaces a section in a template with a value
|
|
tplCheckMarker($section);
|
|
$start = tplMakeSectionMarker($section, "START");
|
|
$end = tplMakeSectionMarker($section, "END");
|
|
$start = strpos($tpl, $start);
|
|
$end = strrpos($tpl, $end)+strlen($end);
|
|
if (($start===false) || ($end===false) || ($end<=$start)) return $tpl;
|
|
$ret = ($start>0 ? substr($tpl, 0, $start) : "");
|
|
$ret.= $value;
|
|
if ($end<strlen($tpl)) $ret.= substr($tpl, $end);
|
|
return $ret;
|
|
}
|
|
|
|
function tplReplCondition($tpl, $section, $bool)
|
|
{ // selects one of two sections in a template
|
|
tplCheckMarker($section);
|
|
$mif = tplMakeSectionMarker($section, "IF");
|
|
$melse = tplMakeSectionMarker($section, "ELSE");
|
|
$mend = tplMakeSectionMarker($section, "END");
|
|
|
|
$start = strpos($tpl, $mif);
|
|
$else_start = strpos($tpl, $melse);
|
|
$else_end = $else_start + strlen($melse);
|
|
$end = strrpos($tpl, $mend)+strlen($mend);
|
|
|
|
if (($start===false) || ($else_start == false) || ($end===false) || ($end<=$start)) return $tpl;
|
|
|
|
$value = "";
|
|
if ($bool) {
|
|
// Show if-part
|
|
$value = substr($tpl, $start,$else_end-$start);
|
|
} else {
|
|
// Show else-part
|
|
$value = substr($tpl, $else_start,$end-$else_start);
|
|
}
|
|
|
|
$ret = ($start>0 ? substr($tpl, 0, $start) : "");
|
|
$ret.= $value;
|
|
if ($end<strlen($tpl)) $ret.= substr($tpl, $end);
|
|
return $ret;
|
|
}
|
|
|
|
function tplReplMarker($tpl, $marker, $value)
|
|
{ //replaces a marker in a template with a value
|
|
tplCheckMarker($marker);
|
|
return str_replace($marker, $value, $tpl);
|
|
}
|
|
|
|
function tplReplMarkerArray($tpl, $array)
|
|
{ //replaces all markers in a template with values from an array (index defines marker)
|
|
foreach ($array as $ind => $val)
|
|
{
|
|
tplCheckMarker($ind);
|
|
$tpl = str_replace($ind, $val, $tpl);
|
|
}
|
|
return $tpl;
|
|
}
|