App/lib/20_template.php
2020-11-25 17:28:45 +01:00

75 lines
2.4 KiB
PHP

<?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 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;
}
?>