rendered paste body<?php
class Template {
var $variables;
var $tempname;
var $template;
function Template($tempname) {
$this->tempname = $tempname;
$this->variables = array();
}
function addvar($varname, $varvalue) {
$this->variables[$varname] = $varvalue;
}
function parseloops() {
$loopcount = 0;
$loopcount = preg_match_all("/\{loop=(.*)\}/", $this->template, $loops, PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
for ($i = 0; $i < count($loops); $i++) {
$loopstart = ($loops[$i][0][1] + strlen($loops[$i][0][0]));
$looprepstart = $loops[$i][0][1];
$loopend = (strpos($this->template, "{/loop}", $loopstart));
$looprepend = $loopend + strlen("{/loop}");
$looprep = substr($this->template, $loopstart, ($loopend - $loopstart));
$looprep = $this->parsevars($looprep, $loops[$i][1][0]);
$this->template = str_replace(substr($this->template, $looprepstart, ($looprepend - $looprepstart)), (is_array($looprep)?implode("", $looprep):""), $this->template);
}
}
function parsevars($section, $sub = "") {
if ($sub == "") {
$return = $section;
foreach ($this->variables as $find => $rep) {
if (!is_array($this->variables[$find]))
$return = str_ireplace("{".$find."}", $rep, $return);
}
} else {
if (is_array($this->variables[$sub])) {
$return = array();
for ($i = 0; $i < count($this->variables[$sub]); $i++) {
$tmp = $section;
foreach ($this->variables[$sub][$i] as $find => $rep) {
if (!is_array($this->variables[$sub][$i][$find]))
$tmp = str_ireplace("{".$sub.".".$find."}", $rep, $tmp);
}
$return[] = $tmp;
}
} else {
$section = "Error, unknown loop variable, please contact the system administrator.";
}
}
return $return;
}
function delvars() {
unset($this->variables);
$this->variables = array();
}
function display($tempname) {
$file = "./template/".$this->tempname."/".$tempname;
if (file_exists($file)) {
$this->template = file_get_contents($file);
$this->parseloops();
$this->template = $this->parsevars($this->template);
echo $this->template;
} else {
echo "The template that was requested could not be found, please contact the system administrator.";
}
}
}
?>